简体   繁体   English

如何在窗口滚动时上下移动div

[英]how to move div up & down on window scroll

I have a jquery function to move some div up and down while scrolling the page here is the code ->我有一个 jquery 函数可以在滚动页面时上下移动一些 div,这里是代码 ->

$(window).scroll(function() {
  $(".mydiv").css({
    "margin-top": ($(window).scrollTop()) + "px",
    "margin-left": ($(window).scrollLeft()) + "px"
  });
});

This above code only works on one div like ->上面的代码仅适用于一个 div,例如 ->

<div class="mydiv">This div works</div>
<div class="mydiv">This div takes a high distance from above div and goes down</div>

 $(window).scroll(function() { $(".mydiv").css({ "margin-top": ($(window).scrollTop()) + "px", "margin-left": ($(window).scrollLeft()) + "px" }); });
 body { height: 1000px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="mydiv">This div works</div> <div class="mydiv">This div takes a high distance from above div and goes down</div>

You should be using 'position: absolute' and 'top' and 'left' instead of margins.您应该使用 'position: absolute' 和 'top' 和 'left' 而不是边距。 By using margins you are pushing them off each other making them make the page massive.通过使用边距,您将它们相互推开,使页面变得庞大。

$(window).scroll(function() {
  $(".mydiv").css({
    "top": ($(window).scrollTop()) + "px",
    "left": ($(window).scrollLeft()) + "px"
  });
});

See this codepen - http://codepen.io/dmoojunk/pen/JXBaXm请参阅此代码笔 - http://codepen.io/dmoojunk/pen/JXBaXm

Weave: http://kodeweave.sourceforge.net/editor/#3bc95f551a4e9cf7c52418361b4be806编织: http : //kodeweave.sourceforge.net/editor/#3bc95f551a4e9cf7c52418361b4be806

Here's a simple solution.这是一个简单的解决方案。 Replacing .mydiv with #scroll ..mydiv替换#scroll

Although you can do it in CSS.虽然你可以在 CSS 中做到这一点。 So I don't see why you need JS for this.所以我不明白你为什么需要 JS。

 $(window).scroll(function() { $("#scroll").css({ "margin-top": ($(window).scrollTop()) + "px", "margin-left": ($(window).scrollLeft()) + "px" }); });
 body { height: 1176px; padding: 1em; font-size: 13px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="mydiv" id="scroll">This div works</div> <div class="mydiv" id="scroll">This div takes a high distance from above div and goes down</div>

Here's a simple solution just add this js这是一个简单的解决方案,只需添加此 js

 (function() { var delay = false; $(document).on('mousewheel DOMMouseScroll', function(event) { // event.preventDefault(); if(delay) return; delay = true; setTimeout(function(){delay = false},100) var wd = event.originalEvent.wheelDelta || -event.originalEvent.detail; var a= document.getElementsByClassName('mydiv'); if(wd < 0) { for(var i = 0 ; i < a.length ; i++) { var t = a[i].getClientRects()[0].top; if(t >= 40) break; } } else { for(var i = a.length-1 ; i >= 0 ; i--) { var t = a[i].getClientRects()[0].top; if(t < -20) break; } } if(i >= 0 && i < a.length) { $('html,body').animate({ scrollTop: a[i].offsetTop }); } }); })();
 .fake-container{height: 100vh;}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="mydiv" id="scroll">scroll 1</div> <div class="mydiv" id="scroll">scroll 2</div>

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

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