jquery - 显示/隐藏 div jquery 不能在固定位置工作

标签 jquery css

显示/隐藏 divdiv wrapperposition:fixed 时,使用 jquery 不起作用,但是如果,更改为 position:relative show/hide div 恢复工作。

这是 fiddle of position:relative

这是 fiddle of position:fixed div show/hide 没有显示。

var flag = 0;
var leftValue;
$('#button').on('click',function(){
      flag = !flag;
      leftValue = flag ? 100 : 0;
        $('#right').animate({ left: leftValue }, 'slow', function() {
            $('#button').text(function(i,v){
           return v == 'Close' ? 'Menu' : 'Close';
         });
        });
    });

/* show/hide DIV when passed the other div */
       $(document).scroll(function(){
       var vis = ($(document).scrollTop() > ($('.passedMe').offset().top+$('.passedMe').height()));
       $('.showHide').css('display', vis?'':'none')
       });
      /* show/hide DIV when passed the other div */

有没有人有想法...

最佳答案

那是因为你的代码在这里

$(document).scroll(function(){

因此,如果您正在滚动文档body,您的滚动功能 将起作用,但因为您使用了position: fixed body中没有scrollbar,但是scrollbar

<div id="right">
    ...
</div>

这使得 scroll 函数 永远不会被调用,所以你需要改变

$(document).scroll(function(){
    ...
});

$("#right").scroll(function(){    // Depend on the container that use the position: fixed;
    ...
});

这是 Updated Fiddle

关于jquery - 显示/隐藏 div jquery 不能在固定位置工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25719018/

相关文章:

javascript - 为什么这个 Bootstrap 2.3.2 下拉菜单不起作用?

javascript - JQuery: Tabs - 你怎么知道一个选项卡被点击了?

javascript - 使用 Javascript/jQuery 以编程方式关闭 SELECT 下拉列表

css - 处理界面咏叹调间距的问题

css - object-fit 如何与 canvas 元素一起使用?

c# - 如何将面板定位在页面底部

javascript - 更改 iframe 内容 jquery

jquery - 按 data-rel 顺序应用相同的函数

jquery - 一键移动多个元素到不同位置

javascript - 点击我按钮