简体   繁体   English

当窗口滚动时,我想追加一个div并在滚动回顶部时隐藏div?

[英]When window scroll, I want to append a div and hide the div when scroll back to top?

I want to create a function, when user scroll down to the middle, the menu will have a class 'sticky', when the div has class 'sticky', I want to append a div. 我想创建一个函数,当用户向下滚动到中间时,菜单将有一个“粘滞”类,当div具有“粘滞”类时,我想附加一个div。 The issue is that it will keep append the div when I scroll. 问题是,当我滚动时,它将保持追加div。 Second issue is that, when I scroll back to the top, the the append sticky stay. 第二个问题是,当我滚动回到顶部时,附加粘滞物会停留。 help, appreciate. 帮助,感激。

  jQuery(window).scroll(function(){ if(jQuery('.main-menu-top').hasClass('sticky')){ jQuery('.main-menu-top').append('<div id="checkout_sticky"></div>'); }else{ /*I want to hide the checkout_sticky, try hide(), doesn't work*/ }); 

You need to create something that changes once complete. 您需要创建一些一旦完成便会更改的东西。 To do so, 为此,

create a var with the value of true, and then in your if statement check if it is true, if so run function and then in that function change the value of the var to false. 创建一个值为true的var,然后在if语句中检查它是否为true,如果是,则运行函数,然后在该函数中将var的值更改为false。

var test = true;

jQuery(window).scroll(function(){

           if(jQuery('.main-menu-top').hasClass('sticky') && test==true){
test = false;
               jQuery('.main-menu-top').append('<div id="checkout_sticky"></div>');
                   }else{
            /*I want to hide the checkout_sticky, try hide(), doesn't work*/
   });

To make it hide, you could try; 要使其隐藏起来,您可以尝试;

else { 
    $('#checkout_sticky').css("visibility","hidden");  
}

If that doesn't work you can try, 如果那不起作用,您可以尝试,

$('#checkout_sticky').removeAttr('id');

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

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