简体   繁体   English

如何在javascript和/或css中向下滚动时水平移动div

[英]How to horizontally move a div as you scroll down in javascript and or css

Does anybody know how can I get the effect of a carousel like the one in the bottom of this site https://brand.twitch.tv/ ??有谁知道我怎样才能获得像本网站https://brand.twitch.tv/底部那样的轮播效果?

I was using this example I found in codepen for the structure of this section.我正在使用我在 codepen 中找到的这个示例作为本节的结构。

but when trying to add it to my site with a vertical scroll, instead of being one continuous scroll like the one in the twitch site, its just a scroll inside another scroll.但是当尝试使用垂直滚动将其添加到我的站点时,它不是像 twitch 站点中的滚动那样连续滚动,它只是另一个滚动内的滚动。 It feels weird for the user and you can miss it if you scroll down too fast.这对用户来说感觉很奇怪,如果向下滚动太快,你可能会错过它。

Thanks in advance!提前致谢!

https://codepen.io/lemmin/pen/xRyXMZ https://codepen.io/lemmin/pen/xRyXMZ

 body { margin:0; font-family:Georgia; } #container .box { width:100vw; height:100vh; display:inline-block; position:relative; } #container .box > div { width:100px; height:100px; font-size:96px; color:#FFF; position:absolute; top:50%; left:50%; margin:-50px 0 0 -50px; line-height:.7; font-weight:bold; } #container { overflow-y:scroll; overflow-x:hidden; transform: rotate(270deg) translateX(-100%); transform-origin: top left; background-color:#999; position:absolute; width:100vh; height:100vw; } #container2 { transform: rotate(90deg) translateY(-100vh); transform-origin: top left; white-space:nowrap; font-size:0; } .one { background-color: #45CCFF; } .two { background-color: #49E83E; } .three { background-color: #EDDE05; } .four { background-color: #E84B30; }
 <div id="container"> <div id="container2"> <div class="box one"><div>1</div></div> <div class="box two"><div>2</div></div> <div class="box three"><div>3</div></div> <div class="box four"><div>Last</div></div> </div> </div>

One way to achieve this is by using Scrollmagic .实现此目的的一种方法是使用Scrollmagic The learning curve is a little steep but the rewards are worth it.学习曲线有点陡峭,但回报是值得的。

https://codepen.io/sean_pwc/pen/wvaaYWE https://codepen.io/sean_pwc/pen/wvaaYWE

I've forked your pen and amended slightly.我已经分叉了你的笔并稍作修改。 Here's how it works.这是它的工作原理。

1) We add a bunch of divs that we want to be part of the normal page scroll. 1) 我们添加了一堆想要成为普通页面滚动一部分的 div。 Nothing new here.这里没有什么新鲜事。

<div class="scroll-vertical">
   <div class="v-box one">1</div>
   <div class="v-box two">2</div>
   <div class="v-box three">3</div>
   <div class="v-box four">Last</div>
</div>

I've set a height of 300px and the boxes take up full width of the screen.我将高度设置为 300 像素,并且这些框占据了屏幕的整个宽度。 Then we add a section that we'd like to scroll horizontally.然后我们添加一个我们想要水平滚动的部分。 Notice the change in styles on the boxes - flex-direction is set to row, so that the boxes sit next to each other, and we set a width on them.请注意框上样式的变化 - flex-direction 设置为 row,以便框彼此相邻,我们在它们上设置宽度。

<div id="scrollHorizontal">
   <div class="h-box one">1</div>
   <div class="h-box two">2</div>
   <div class="h-box three">3</div>
   <div class="h-box four">4</div>
</div>

The comes the magic.魔法来了。

var controller = new ScrollMagic.Controller();

var scrollHorizontal = new TimelineLite()
  scrollHorizontal.to("#scrollHorizontal", 1, {x:'-100%'})

var horizontalScroll = new ScrollMagic.Scene({
      triggerElement: "#scrollHorizontal",
      triggerHook: 'onLeave',
      duration: 3000
    }).setPin("#scrollHorizontal").setTween(scrollHorizontal).addTo(controller);

I recommend you read the docs and play around with the demos to figure out whats going on.我建议您阅读文档并使用演示来弄清楚发生了什么。 But essentially you set a controller which contains your animations.但本质上,您设置了一个包含动画的控制器。

var controller = new ScrollMagic.Controller();

Then we target the element we would like to move.然后我们定位我们想要移动的元素。 Here, we target #scrollHorizontal, which is the wrapper for the h-boxes, and then we tell it move itself all the way over to the left until it is off the screen.在这里,我们的目标是#scrollHorizo​​ntal,它是 h-box 的包装器,然后我们告诉它一直向左移动,直到它离开屏幕。 Like you would position a side nav off the screen.就像您将侧面导航放在屏幕外一样。

var scrollHorizontal = new TimelineLite()
  scrollHorizontal.to("#scrollHorizontal", 1, {x:'-100%'})

Now this will work, but the vertical scroll will still take effect, and it won't feel great.现在这样可以了,但是垂直滚动还是会生效,感觉不会太好。 So we pin the element to be scrolled to the top of the screen - essentially scrollmagic adds a bunch of whitespace (set by the duration key, which is a height in pixels), which the user scrolls through, but it is hidden behind the pin, and we just see whatever you set to happen in scrollHorizontal (which was to move completely to the left).因此,我们将要滚动的元素固定在屏幕顶部——本质上,scrollmagic 添加了一堆空白(由持续时间键设置,以像素为单位的高度),用户可以滚动这些空白,但它隐藏在 pin 后面,我们只看到您在 scrollHorizo​​ntal 中设置的任何内容(完全向左移动)。

var horizontalScroll = new ScrollMagic.Scene({
      triggerElement: "#scrollHorizontal",
      triggerHook: 'onLeave',
      duration: 3000
    }).setPin("#scrollHorizontal").setTween(scrollHorizontal).addTo(controller);

So we set a target element, and when the browser detects it, the element is pinned, the animation you declared in newTimelineLite() is actioned, for the duration you set, and then when the duration is over, we un-pin and go back to a natural vertical scroll.所以我们设置了一个目标元素,当浏览器检测到它时,该元素被固定,你在 newTimelineLite() 中声明的动画被动作,在你设置的持续时间内,然后当持续时间结束时,我们取消固定并继续回到自然的垂直滚动。 Play around with duration to change the speed of the horizontal scroll.玩弄持续时间来改变水平滚动的速度。

References:参考:

https://scrollmagic.io/ https://scrollmagic.io/docs/index.html https://scrollmagic.io/ https://scrollmagic.io/docs/index.html

EDIT编辑

I should add, the codepen is using 4 scripts:我应该补充一下,codepen 正在使用 4 个脚本:

https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js
https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TweenMax.min.js
https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/ScrollMagic.min.js
https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/plugins/animation.gsap.min.js

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

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