简体   繁体   English

当用户使用jQuery滚动时,在不同的div中触发滚动事件

[英]Trigger a scrolling event in a different div when a user scrolls using jQuery

I have two div elements: 我有两个div元素:

When a user scrolls div #element-A and #header-one-target reaches the top of the containing div the last element ( #animate-hd-b ) in #element-B should scroll to the top of the containing div with a nice animation . 当用户滚动div #element-A并且#header-one-target到达包含div的顶部时, #element-B的最后一个元素( #animate-hd-b )应该滚动到包含div的顶部好动画。

Here's the code that I'm working with to start. 这是我正在使用的代码。 The code below does something when the window is scrolled not the div. 当窗口滚动而不是div时,下面的代码会执行某些操作。

 $(window).scroll(function() { var offsetTop = $('#animate-hd-b').offset().top, outerHeight = $('#animate-hd-b').outerHeight(), windowHeight = $(window).height(), scrollTop = $(this).scrollTop(); console.log((offsetTop-windowHeight) , scrollTop); if (scrollTop > (offsetTop+outerHeight-windowHeight)){ alert('you have scrolled to the top!'); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="element-A" style="background: orange; overflow: auto;"> <div class="content" style="padding-bottom: 300px;"> <p>content</p> <p>content</p> <p>content</p> <p>content</p> <h1 id="header-one-target">Header One</h1> </div> </div> <div id="element-B" style="background: yellow; overflow: auto;"> <div class="content" style="padding-bottom: 300px;"> <p>content</p> <p>content</p> <p>content</p> <p>content</p> <h1 id="animate-hd-b">Animate This Header</h1> </div> </div> 

Is there a way to do this in jQuery? 有没有办法在jQuery中执行此操作?

This is really pretty simple. 这真的很简单。 You just keep track of #header-one-target and animate #animate-hd-b when #header-one-target reaches at the top. #header-one-target到达顶部时,您只需跟踪#header-one-target和animate #animate-hd-b

 (function($) { let $elementA = $('#element-A'); let $elementB = $('#element-B'); let $headerOneTarget = $('#header-one-target'); let $animateHdB = $('#animate-hd-b'); let isScrollAtTop = true; $elementA.scroll(function() { if (isScrollAtTop && $headerOneTarget.offset().top < 5) { isScrollAtTop = false; $elementB.animate({ scrollTop: $elementB.scrollTop() + $animateHdB.offset().top }); } else if ($elementA.scrollTop() < 5) { isScrollAtTop = true; $elementB.animate({ scrollTop: 0 }); } }); })(jQuery); 
 #element-A { background: orange; overflow: auto; height: 100vh; width: 60vw; position: fixed; top: 0; left: 0; } #element-B { position: fixed; top: 0; right: 0; height: 100vh; width: 40vw; background: yellow; overflow: auto; } .content { padding: 10px; } .content-vh100 { height: 100vh; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="element-A"> <div class="content"> <p>Scroll</p> <p>to</p> <p>header</p> <p>one</p> <h1 id="header-one-target">Header One</h1> <div class="content-vh100"></div> </div> </div> <div id="element-B"> <div class="content"> <p>to</p> <p>animate</p> <p>following</p> <p>content</p> <h1 id="animate-hd-b">Animate This Header</h1> <div class="content-vh100"></div> </div> </div> 

Some conditions were added that should prevent unnecessary animations and queueing (which tends to happen when listening for scroll and animating scrollTop ). 添加了一些条件可以防止不必要的动画和排队(这在倾听scroll和动画scrollTop时会发生)。 It keeps track of the scroll direction and won't start animating when the element on the right has already reached its position. 它跟踪滚动方向,并且当右侧元素已经到达其位置时不会开始动画。

Codepen demo Codepen演示

 var sin = $('#element-A'), dex = $('#element-B'), peg = sin.scrollTop(); sin.scroll(function() { var way = sin.scrollTop(), rate = Math.round(sin.find('h1').position().top), area = dex.scrollTop(), turf = Math.round(dex.find('h1').position().top), down = way > peg; peg = way; // conditions for scrolling down if (rate < 0 && down && turf) { dex.not(':animated').animate({scrollTop: area+turf}, 700); } // scrolling up if (!down && area) { dex.not(':animated').animate({scrollTop: 0}, 700); } }); 
 body { margin: 0; } body > div { width: 50%; height: 100vh; float: left; overflow: auto; } #element-A { background: orange; } #element-B { background: yellow; } .content { padding-bottom: 100vh; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="element-A"> <div class="content"> <p>content</p> <p>content</p> <p>content</p> <p>content</p> <h1 id="header-one-target">Header One</h1> </div> </div> <div id="element-B"> <div class="content"> <p>content</p> <p>content</p> <p>content</p> <p>content</p> <h1 id="animate-hd-b">Animate This Header</h1> </div> </div> 

In case the elements are differently positioned in the target environment, using position() is a more straightforward approach than offset() because the latter is relative to the document. 如果元素在目标环境中的position()不同,则使用position()是一种比offset()更简单的方法,因为后者是相对于文档的。 The former (used here) is relative to its own parent element and should work independent of its position. 前者(此处使用)与其自己的父元素相关,应独立于其位置。

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

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