简体   繁体   English

当我们滚动到特定位置时为 div 设置动画

[英]Animate a div when we scroll to a specific position

I have a function that decrease the width of the slider-side-hr to 0 when scrolling from the top of the page to -600 of the scrolling div height (which means before reaching the bottom of the scrolling div ).我有一个函数,当从页面顶部滚动到scrolling div高度的 -600(这意味着在到达scrolling div的底部之前)时,将slider-side-hr的宽度减小到 0。
The slider sides are both fixed, one to right and the other to the left.滑块两侧都是固定的,一个在右边,另一个在左边。

  <div class="tower" id="tower2">
      <div class="scroll-slider-hr">
          <div class="slider-side-hr slider-side1"></div>
          <div class="slider-side-hr slider-side2"></div>
      </div>
  </div>

It works perfectly fine and the width of both slider-side-hr decreasing to 0 when scrolling, but I have a problem when the scrolling div is NOT on the top of the page.它运行完美,两者的宽度slider-side-hr滚动时降低到0,但我有一个问题,当scrolling div在页面的顶部。

I need a condition that executes the function ONLY when ( scrolling div ) reaches the bottom of the page + 100px , which means I can see the slider-side-hr full width first (with 100px of the scrolling div height ) then it starts to decrease to 0 on scrolling, and it should decrease to 0 when reaching half of the scrolling div我需要一个仅在( scrolling div )到达页面底部 + 100px时才执行该函数的条件,这意味着我可以首先看到slider-side-hr全宽( scrolling div height为 100 100px )然后它开始滚动时减少到 0,当达到scrolling div一半时它应该减少到 0

 var $scrollingDiv = $("#tower2"); $(window).scroll(function() { var winScrollTop = ($(document).scrollTop() + 0); var zeroSizeHeight = $scrollingDiv.height() - 600; var newSize = 250 * (1 - (winScrollTop / zeroSizeHeight)); $('.slider-side-hr').css({ width: newSize, }, 500, 'easeInOutSine'); });
 .container-full { width: 100%; margin-right: auto; margin-left: auto; } .tower { position: relative; } #tower1 { /*margin-bottom: 700px*/ } #tower2 { background-image: linear-gradient(rgba(0, 0, 0, 0.5),rgba(0, 0, 0, 0.5)); background-position: center; background-size: cover; background-repeat: no-repeat; height: 140vh; background-attachment: fixed; position: relative; overflow: hidden; } .slider-side-hr { position: absolute; top: 0; bottom: 0; width: 250px; height: 100%; background: #ddd; } .slider-side1 { left: 0; } .slider-side2 { right: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <dic class="container-full"> <div class="tower" id="tower1"> </div> <div class="tower" id="tower2"> <div class="scroll-slider-hr"> <div class="slider-side-hr slider-side1"></div> <div class="slider-side-hr slider-side2"></div> </div> </div> </div>

How this code works:此代码的工作原理:

JS JS

Takes information about the location from top of #tower2 and assigns it to the variable ftop#tower2顶部获取有关位置的信息并将其分配给变量ftop

From ftop we subtract the scrolling value.我们从ftop减去滚动值。 When this value becomes less than 0, the transmission of a new value to .slider-side-hr begins.当该值小于 0 时,开始向.slider-side-hr传输新值。

The initial value of the width of .slider-side-hr is set to 50% of the width of #tower2 .slider-side-hr的宽度初始值设置为#tower2宽度的50%

If you want to change the "animation" speed ... change this line:如果你想改变“动画”速度......改变这一行:

var newSize = zeroSizeWidth + scl; to this var newSize = zeroSizeWidth + scl * 2;到这个var newSize = zeroSizeWidth + scl * 2;

(If you want the animation to be responsive for desktop and mobile you can make the value * 2 to be a percentage of the screen width) (如果您希望动画响应桌面和移动设备,您可以将值* 2设为屏幕宽度的百分比)

CSS CSS

The change made is of class .slider-side-hr value width: 100%;所做的更改是类.slider-side-hrwidth: 100%;

I hope I've been helpful我希望我有所帮助

 var $scrollingDiv = $("#tower2"); var ftop = $scrollingDiv.offset().top; var zeroSizeHeight = $scrollingDiv.height(); var zeroSizeWidth = $scrollingDiv.width() / 2; $(window).scroll(function () { var winScrollTop = $(window).scrollTop(); var scl = ftop - winScrollTop; if (scl < 0) { var newSize = zeroSizeWidth + scl * 2; } else { var newSize = zeroSizeWidth; } $('.slider-side-hr').css({ width: newSize, }, 500, 'easeInOutSine'); });
 .container-full { width: 100%; margin-right: auto; margin-left: auto; } .tower { position: relative; } #tower1 { /*margin-bottom: 700px*/ } #tower2 { background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)); background-position: center; background-size: cover; background-repeat: no-repeat; height: 140vh; background-attachment: fixed; position: relative; overflow: hidden; } .slider-side-hr { position: absolute; top: 0; bottom: 0; width: 100%; height: 100%; background: #ddd; } .slider-side1 { left: 0; } .slider-side2 { right: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <div class="container-full"> <div class="tower" id="tower1"> Lorem, ipsum dolor sit amet consectetur adipisicing elit. Consequatur ipsam fuga amet quaerat possimus qui impedit iure id. Accusantium expedita architecto doloribus ratione veniam itaque in iure assumenda ab? Obcaecati! Lorem, ipsum dolor sit amet consectetur adipisicing elit. Consequatur ipsam fuga amet quaerat possimus qui impedit iure id. Accusantium expedita architecto doloribus ratione veniam itaque in iure assumenda ab? Obcaecati! Lorem, ipsum dolor sit amet consectetur adipisicing elit. Consequatur ipsam fuga amet quaerat possimus qui impedit iure id. Accusantium expedita architecto doloribus ratione veniam itaque in iure assumenda ab? Obcaecati! Lorem, ipsum dolor sit amet consectetur adipisicing elit. Consequatur ipsam fuga amet quaerat possimus qui impedit iure id. Accusantium expedita architecto doloribus ratione veniam itaque in iure assumenda ab? Obcaecati! Lorem, ipsum dolor sit amet consectetur adipisicing elit. Consequatur ipsam fuga amet quaerat possimus qui impedit iure id. Accusantium expedita architecto doloribus ratione veniam itaque in iure assumenda ab? Obcaecati! Lorem, ipsum dolor sit amet consectetur adipisicing elit. Consequatur ipsam fuga amet quaerat possimus qui impedit iure id. Accusantium expedita architecto doloribus ratione veniam itaque in iure assumenda ab? Obcaecati! Lorem, ipsum dolor sit amet consectetur adipisicing elit. Consequatur ipsam fuga amet quaerat possimus qui impedit iure id. Accusantium expedita architecto doloribus ratione veniam itaque in iure assumenda ab? Obcaecati! Lorem, ipsum dolor sit amet consectetur adipisicing elit. Consequatur ipsam fuga amet quaerat possimus qui impedit iure id. Accusantium expedita architecto doloribus ratione veniam itaque in iure assumenda ab? Obcaecati! Lorem, ipsum dolor sit. </div> <div class="tower" id="tower2"> <div class="scroll-slider-hr"> <div class="slider-side-hr slider-side1"></div> <div class="slider-side-hr slider-side2"></div> </div> </div> </div>

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

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