2011-12-11 22 views
-3

高さが1000ピクセルを超えるWebページがあります。私は常に訪問者に表示する必要がある重要なテキストがあります。私はページの上部に固定プロパティを持つ20ピクセルの高さのDIVを配置しましたが、そのDIVの内容はブラウザの中に表示されます。私は一番上のdivを隠したいと思っていますが、中のdivから上にスクロールすると、一番上のdivが表示されます。要素がブラウザに表示されている間divを表示しない

+2

お元気ですか、あなたの質問は非常に不明です。ちょっと書き直して、これまでにやったことのいくつかのサンプルを提供できますか? –

+0

これは、アイテムが画面上にあるかどうかの検出に役立ちます - http://stackoverflow.com/questions/487073/jquery-check-if-element-is-visible-after-scroling-特に[この回答]( http://stackoverflow.com/a/3535028/232593) –

+0

私は彼がヘッダーをどちらかにしたいと思うと思います。1.ユーザーが「折りたたみ」の下にあるとき、つまり折りたたみが表示の最上部にあるときまたは2.ビューが一番上までスクロールされていないとき。それは間違いない、Sumit? – Garvin

答えて

0

あなたはこのようなものをお探しですか?それが表示されていないときは、span#interestingが表示されているときのdivを表示するには、このJavaScriptを使用して、それを隠すことができ

<p>a bunch of text, and duplicate this several times. I used lorem ipsum</p> 
<p><span id="interesting">Here is the interesting text.</span></p> 
<p>a bunch more text, and duplicate this several times. I used lorem ipsum</p> 

:このHTML考える

// Add a div to contain a copy of the interesting text 
var interestingOffscreen = $('<div/>') 
    .attr('id', 'interesting') 
    .text($("span#interesting").text()); 

$('body').prepend(interestingOffscreen); 

// Center the display of that copy 
interestingOffscreen.css(
    'margin-left', 
    -(interestingOffscreen.outerWidth()/2) 
); 

// Detect when it is offscreen/onscreen and react  
function isScrolledIntoView(elem) 
{ 
    var docViewTop = $(window).scrollTop(); 
    var docViewBottom = docViewTop + $(window).height(); 

    var elemTop = $(elem).offset().top; 
    var elemBottom = elemTop + $(elem).height(); 

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)); 
} 

var isNotVisibleHandler; // forward declaration 

function isVisibleHandler() 
{ 
    if(isScrolledIntoView($("span#interesting"))) 
    { 
     $(window).unbind('scroll'); 
     interestingOffscreen.fadeOut(
      function() { 
       $(window).scroll(isNotVisibleHandler); 
       $(window).scroll(); // force detection 
      } 
     ); 
    } 
} 

isNotVisibleHandler = function() 
{ 
    if(!isScrolledIntoView($("span#interesting"))) 
    { 
     $(window).unbind('scroll'); 
     interestingOffscreen.fadeIn(
      function() { 
       $(window).scroll(isVisibleHandler); 
       $(window).scroll(); // force detection 
      } 
     ); 
    } 
}; 

$(window).scroll(isVisibleHandler); 

このすべてが厳密に必要なわけではありませんが、私は見た目は冷静です。

関連する問題