[英]How to combine both of these IF statements into one?
I want to run an alert if the viewport is wider than 1250px and if the user is near the bottom of the page. 如果视口宽于1250像素并且用户位于页面底部附近,我想发出警报。
I currently have the two snippets of code which work individually... 我目前有两个代码段,分别工作。
function checkSize(){
if ($(window).width() > 1250) {
alert('hello');
}
}
checkSize();
$(window).resize(function() {
checkSize();
});
and the other snippet which detects how far the user has scrolled down... 另一个用于检测用户向下滚动距离的代码段...
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - 700) {
alert('hello');
}
});
How can I combine the two in one if statement? 我如何将两者合二为一的if语句? I have tried this but it does not seem to work..
我已经尝试过了,但是似乎没有用。
function checkSize(){
if ($(window).width() > 1250 && $(window).scrollTop() + $(window).height() > $(document).height() - 700) {
alert('hello');
}
}
checkSize();
$(window).resize(function() {
checkSize();
});
(($(window).width() > 1250) && ($(window).scrollTop() + $(window).height()) > $(document).height() - 700)
您需要放置()
以显示您要计算的内容!
Following discussion in comments. 在评论中进行讨论。 This seems to be working:
这似乎正在工作:
function checkSize(){
if($(window).width() > 1250 && $(window).scrollTop() + $(window).height() > $(document).height() - 700) {
console.log('hello');
}
}
checkSize();
$(window).scroll(checkSize);
$(window).resize(checkSize);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.