在页面中间传递时触发JQuery函数 [英] Trigger JQuery function when passed half way down the page

查看:195
本文介绍了在页面中间传递时触发JQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法告诉您是否已滚动浏览网页的中心,或者换句话说,当您滚动时正好通过网页的一半而滚动条位于浏览器窗口的下半部分?

Is there a way to tell if you have scrolled passed the center of the web page or in other words, when you have scrolled passed exactly half of the web page and your scrollbar is situated in the lower half of the browser window?

我希望能够触发这个:
$('。pineapple-man')。show();当我向下滚过页面的一半时?

I want to be able to trigger this: $('.pineapple-man').show(); when I have scrolled down passed half of the page?

这可能吗?

你的帮助会太善良了!

Your help would be so kind!

推荐答案

您可以使用 .scrollTop()<来滚动元素的像素数量/ code>。要收听滚动事件,请使用 .scroll()

如果要识别中途,请使用高度滚动:

When you want to identify the halfway, use height of the scroll:

$(window).scroll(function () { 
  if ($(window).scrollTop() > $('body').height() / 2) {
    $('.pineapple-man').show();
  } 
});

如果您要滚动除整个窗口/正文之外的其他元素,请随时更改选择器。

If you are scrolling some other element than the whole window/body, please feel free to change the selectors.

要显示一个计时器,请添加删除滚动事件监听器,方法是添加以下内容 .show()致电:

To make the showing one-timer, add the removal of scroll event listener, by adding the following after the .show() call:

$(window).unbind('scroll');

这篇关于在页面中间传递时触发JQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆