Javascript 检查元素是否只在视口中出现一次,jQuery

Javascript 检查元素是否只在视口中出现一次,jQuery,javascript,jquery,html,css,Javascript,Jquery,Html,Css,当此div位于视口中时,我想在div内增加/减少图像的左边距: function onScroll(event){ var scrollW = $(this) $('div.my').each(function(){ if ($(this).is(":in-viewport")) { var st = scrollW.scrollTop(); if (st > lastScrollTop){

当此
div
位于视口中时,我想在
div
内增加/减少图像的
左边距

function onScroll(event){
 var scrollW = $(this)
  $('div.my').each(function(){
        if ($(this).is(":in-viewport")) {
                 var st = scrollW.scrollTop();
                if (st > lastScrollTop){
                    //when scrolled to bottom
                    $("div.my img").css('margin-left', '+=20px');

                } else {
                     //when scrolled to top
                     $("div.my img").css('margin-left', '-=20px');               
                }
                lastScrollTop = st;

                //need to stop here
        }
    });
}
一切都很好,除了我想改变一次20像素的边距,当滚动到底部和一次时,滚动到顶部

非常感谢您的帮助。

您是否尝试过if(st=$(document.height()){//bottom}您是否尝试过if(st=$(document.height()){//bottom}