JS window.addEventListener仅适用于一页
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS window.addEventListener仅适用于一页相关的知识,希望对你有一定的参考价值。
我有一个javascript,如果元素在屏幕上,则由window.addEventListener触发。当查看特定的div时,会将CSS类(来自Animate CSS)添加到div元素。该代码仅在我网站的一页上有效,而在其他网页上则无效。
我仅在第二页上发现此控制台错误(JS不起作用)
TypeError: $elem.offset(...) is undefined
Live Project URL:
// Element In View function
function isOnScreen(elem)
// if the element doesn't exist, abort
if (elem.length == 0)
return;
var $window = jQuery(window)
var viewport_top = $window.scrollTop()
var viewport_height = $window.height()
var viewport_bottom = viewport_top + viewport_height
var $elem = jQuery(elem)
var top = $elem.offset().top
var height = $elem.height()
var bottom = top + height
return (bottom > viewport_top) && (top < viewport_bottom)
// Animation when element is in view
window.addEventListener('scroll', function (e)
if (isOnScreen('#lfdesign'))
$("#lfdesign .flexcontainer").addClass("animated slower bounceInRight");
if (isOnScreen('#books'))
$("#books .flexcontainer").addClass("animated slower bounceInLeft");
if (isOnScreen('.srp-color'))
$(".srp-color .flexcontainer").addClass("animated slower bounceInRight");
if (isOnScreen('.nescol-color'))
$(".nescol-color .flexcontainer").addClass("animated slower bounceInLeft");
if (isOnScreen('.worldskills-color'))
$(".worldskills-color .flexcontainer").addClass("animated slower bounceInRight");
if (isOnScreen('.silvernote-color'))
$(".silvernote-color .flexcontainer").addClass("animated slower bounceInLeft");
);
CSS
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css">
<section class="left silvernote-color">
<div class="flexcontainer">
<div class="flex-item">
<a href="webdesign/silvernote">
<img class="normal" src="img/silvernote-logo.png" alt="The logo for SilverNote, a music enterprise hub in Aberdeen who I helped to create a responsive site for by adding artist profiles with a team of developers">
</a>
</div>
<div class="flex-item">
<p>SilverNote Music is a music enterprise hub in Aberdeen that want to promote new music. They tasked me and three others to work as a team to create a responsive site that catered to those viewing on computers as well as a younger, mobile audience.</p>
<p>My main responsibilities were communicating with our client and developing the artist page. I pitched the prototype to the wider organisation and gathered feedback from the band members. This feedback informed the music players I created on the artist profiles.</p>
</div>
</div>
</section>
isOnScreen
将字符串作为参数,并检查该length
的string
是否为0
-这实际上并不检查该元素是否在页面上。function isOnScreen(elem)
if (elem.length == 0)
return
window.addEventListener(`scroll`, function (e)
if (isOnScreen(`#lfdesign`)
// passing a string and checking it's length
// inside the `isOnScreen` function doesn't tell
// you if this element is on the page or not
)
相反,应该做的是先尝试使用jQuery
来抓取/查找元素,如果您愿意的话,然后检查是否存在。如果是这样,请执行您需要做的所有事情,否则请尽早return
。
function isOnScreen(selector) var el = $(selector) if (el.length !== 1) console.log(`element $selector does not exist!`) return; console.log(`element $selector exists!`) // do work // inside of `window.addEventListener` callback function if (isOnScreen("#lfdesign")) // do work if (isOnScreen("#books")) // do work if (isOnScreen(".srp-color")) // do work
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="lfdesign"> <h4>My ID is <b>lfdesign</b>!</h4> </div> <div id="books"> <h4>My ID is <b>books</b>!</h4> </div>
以上是关于JS window.addEventListener仅适用于一页的主要内容,如果未能解决你的问题,请参考以下文章
比较Backbone.js, Angular.js, Ember.js, Knockout.js