简体   繁体   English

CSS JS显示无更改div

[英]CSS JS Display none alter div

I have a one pager. 我有一个传呼机。 And in that one pager, I have an item that is set as display:none (fixed side nav in a div). 在那个传呼机中,我有一个设置为display:none (在div中固定了侧面导航)。

Can I have it show when scrolling to a certain div? 滚动到某个div时可以显示吗?

So it starts out in the code but not displayed, and then when the user scrolls to #about can the side nav show up? 因此,它从代码开始但没有显示,然后当用户滚动到#about时, #about可以显示侧面导航?

Essentially you will need to check if the user has scrolled to or beyond the div id of about. 基本上,您将需要检查用户是否滚动到div左右或超出div左右。 First you will need to establish the current Y value of the div. 首先,您需要确定div的当前Y值。

//cache about div
var about = $('#about');
//this caches the about div position on window load
var aboutPosition = about.position();

Next you will need to determine how far the the user has scrolled. 接下来,您将需要确定用户滚动了多远。 The best way I have determined to accomplish this is with a timer. 我确定要实现这一目标的最佳方法是使用计时器。 You could use the scoll event but its far too taxing on the user browser and a timer will be for the most part indistinguishable. 您可以使用scoll事件,但它在用户浏览器上的负担太重,计时器在大多数情况下是无法区分的。

//generic timer set to repeat every 10 mili(very fast) 
//with a callback to execute the logic
var checkScrollPos = window.setInterval("scrollTopLogic()",10);

function scrollTopLogic(){
    //if about y position is greater than or equal to the 
    //current window scroll position do something
    if(aboutPosition.y >= $(window).scrollTop()){
        $('nav').show();
        //remove timer since it is no longer needed
        window.clearInterval(checkScrollPos);
    }
}

You can catch the scroll event of the div and show the element like this 您可以捕获div的滚动事件并显示如下所示的元素

$("#div").scroll(function() {
   $("#item").show();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM