2016-09-16 7 views
0

私はiWebを使用しています(恐ろしいですが、私は長い話をしています)。iWeb iFrameとスムーズなスクロールの問題

私はページ上で滑らかなスクロールリンクを得ることができましたが、正しい位置にスクロールすることができません。ここで

はインラインフレームにロードされ、「HTMLウィジェット」(FYIこれはメニューである)のために私のコードです:

<html> 
<head> 
<script type="text/javascript"> 

// the var's are referneces to iWeb's generated div's have to publish and get id's 
    var about  = "id1"; 
    var products = "id4"; 
    var technical = "id7"; 
// var contactus = "id14"; 

$(function() { 
    $('a[href*="#"]:not([href="#"])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 

     if (target.length) { 
     $('html, body', window.parent.document).animate({ 
      //scrollTop: parent.document.getElementById(products).offsetTop // this works but is a static ref 
      scrollTop: parent.document.getElementById(theTarget).offsetTop 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
}); 

</script> 
</head> 
<body> 
    <div style="width: "100%"; height: "0%""> 
    <a href="#about" id="about" class="myButton">About</a> 
    <a href="#products" id="products" class="myButton">Products</a> 
    <a href="#technical" id="technical" class="myButton">Technical</a> 
    <a href="#contactus" id="contactus" class="myButton">Contact</a> 
    </div> 
</body> 
</html> 

私はリンク上でこのクリックを表示したときに今、それだけでロードされますメインウィンドウをスクロールせずにiframeページを表示します。

しかし、他のScrollTop行のコメント/コメントを外しても機能するが、明らかに親ウィンドウの "id4" divにスクロールするだけだ。

どうすればこの機能を、リンクごとに同じ機能を何度もコピーしたり貼り付けたりせずに済ませることができますか?

答えて

0

私は要素IDへのリンクハッシュをマッピングするために、辞書の並べ替えをお勧め:

var map = { 
    '#about':  'id1', 
    '#products': 'id4', 
    '#technical': 'id7', 
    '#contactus': 'id14', 
}; 

あなたは、そのマップのキーとしてtargetを使用することができますこの方法:幻想厥

if (target.length && target in map) { 
    $('html, body', window.parent.document).animate({ 
     scrollTop: parent.document.getElementById(map[target]).offsetTop, 
    }, 1000); 
    return false; 
} 
0

、それは最初にはうまくいきませんでしたが、ここでは意図したとおりに完全に動作するコードを使用しています:

<script type="text/javascript"> 
var map = { 
    'about':  'id1', 
    'products': 'id4', 
    'technical': 'id7', 
    'contactus': 'id11', 
}; 

$(function() { 
    $('a[href*="#"]:not([href="#"])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 

     var target = $(this.hash); 

     if (target.length) { 
     $('html, body', window.parent.document).animate({ 
      scrollTop: parent.document.getElementById(map[this.hash.slice(1)]).offsetTop 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
}); 
</script> 
関連する問題