简体   繁体   English

显示/隐藏类,取决于是否有垂直滚动条

[英]Show/hide class depending on if there is a vertical scrollbar or not

I'm new to jQuery and have been trying to get this to work but have been unsuccessful. 我是jQuery的新手,一直在尝试使它正常工作,但未成功。 I can get portions of my code working, but haven't been able to get it all working together. 我可以使部分代码正常工作,但无法使其全部正常工作。

On my website, I have an up and down arrow (both inside a single parent class) that will scroll to the top/bottom of the page when clicked -- I have this working already using jQuery. 在我的网站上,我有一个向上和向下箭头(都在单个父类中),单击时会滚动到页面的顶部/底部-我已经在使用jQuery。

However, I only want these arrows to be displayed if there's actually anything to scroll (ie if there's a scrollbar present); 不过,我只是,如果确实有什么滚动来显示这些箭头(即,如果有一个当前滚动条); otherwise I want them to be hidden. 否则我希望它们被隐藏。

(function($) {
    $.fn.hasScrollBar = function() {
        return this.get(0).scrollHeight > this.height();
    }
})(jQuery);

That code above will return true or false depending on the presence of a scrollbar, but I haven't been able to get it working with my function (I don't have to use that code, it's just what I found). 上面的代码会根据滚动条的存在而返回true或false,但是我无法使其与我的函数一起工作(我不必使用该代码,这就是我所找到的)。

window.onresize=arrows;
window.onload=arrows;

function arrows(){
if(document.body.scrollHeight > window.innerHeight){
document.getElementById("arrow").style.display="block";
}else{
document.getElementById("arrow").style.display="none";
}
}

Not jquery but its working (or not :)) 不是jQuery的,但它的工作(或不:))

我会在this.height.change(*your function here*)上使用事件侦听器,然后使用返回值触发一个函数,该函数将使用jQuery的showhide来显示/隐藏滚动按钮

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

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