[英]jQuery - dynamically move element horizontally on vertical mouse scroll - scroll method not working on custom variable
My objective is to have the .horizontal-slide element move horizontally from left to right when the parent element (.horizontal-slide-wrapper) scrolls into view, just like this: http://jsfiddle.net/PvVdq/ . 我的目标是当父元素(.horizontal-slide-wrapper)滚动到视图中时,.horizontal-slide元素从左向右水平移动,就像这样: http : //jsfiddle.net/PvVdq/ 。
I have used the code snippet above for the basis of my code. 我使用上面的代码片段作为我的代码的基础。 The problem I have is that the desired horizontal sliding behaviour takes place when scroll() method is applied to $(window), but not when appleid to the $wrapper or other custom variables (the jQuery does not activate).
我遇到的问题是当scroll()方法应用于$(window)时会发生所需的水平滑动行为,但是当$ wrap到$ wrapper或其他自定义变量(jQuery没有激活)时则不会。
HTML: HTML:
<div class="horizontal-slide-wrapper">
<h1 class="horizontal-slide">Sliding text</h1>
...
content
...
</div>
Nb - the parent element sits quite far down the page and has height > 100vh. Nb - 父元素位于页面的相当远的位置,高度> 100vh。
CSS: CSS:
.horizontal-slide-wrapper {
position: relative;
}
.horizontal-slide {
position: fixed;
top: 0;
left: 0;
}
jQuery: jQuery的:
$(document).ready(function () {
var $horizontal = $('.horizontal-slide');
var $wrapper = $horizontal.parent();
$(window).scroll(function () {
var s = $(this).scrollTop(),
d = $wrapper.height();
scrollPercent = (s / d);
var position = (scrollPercent * $(document).width());
$horizontal.css({
'left': position
});
});
});
I just need to get this scroll method firing on $wrapper. 我只需要在$ wrapper上触发此滚动方法。 From there I should be fine dynamically adjusting the CSS attribute 'top'.
从那里我应该很好动态调整CSS属性'顶部'。
Thanks in advance for any help. 在此先感谢您的帮助。
Is this what you are after: jsfiddle modified ? 这就是你所追求的: jsfiddle修改 ?
I modified the original fiddle. 我修改了原来的小提琴。 What you need to know is the ratio between
scrollTop
and scrollHeight
. 你需要知道的是
scrollTop
和scrollHeight
之间的比例。 You can access the first one using jQuery ( .scrollTop()
) on the $wrapper
, but you need the native javascript object to access scrollTop
(so: $wrapper[0].scrollHeight
). 你可以在
$wrapper
上使用jQuery( .scrollTop()
)访问第一个,但你需要本机javascript对象来访问scrollTop
(所以: $wrapper[0].scrollHeight
)。 So in the fiddle, the modified code is: 所以在小提琴中,修改后的代码是:
...
$wrapper.scroll(function () {
var s = $wrapper.scrollTop(),
d = $wrapper[0].scrollHeight,
c = $(this).height();
...
Here is an example of the final effect I would like, where the text elements slide horizontally across the viewport when the parent element is in view. 下面是我想要的最终效果的示例,其中当父元素在视图中时,文本元素在视口中水平滑动。
I realised that what I am actually trying to do is just add a horizontal offset to .horizontal-slide
based on the position of the scrollbar s
. 我意识到我实际上要做的只是根据滚动条
s
位置向.horizontal-slide
添加一个水平偏移。
I have managed to do this but cannot seem to get the horizontal offset to line up so that .horizontal-slide
enters the viewport at the same time as its parent .horizontal-slide-wrapper
(ie css left: 0
). 我已设法做到这一点,但似乎无法获得排列的水平偏移,以便
.horizontal-slide
在其父.horizontal-slide-wrapper
(即css left: 0
)的同时进入视口。
It seems that what I have set as $wrapperVerticalOffset
is not calculating what was intended ie the distance between the .horizontal-slide-wrapper
and the top of the document, nor does it stay in constant ratio with $scrollPosition when I change screen resolution or switch in and out of Inspector. 似乎我设置为
$wrapperVerticalOffset
是不计算预期的内容,即.horizontal-slide-wrapper
和文档顶部之间的距离,当我更改屏幕分辨率时,它也不会与$ scrollPosition保持恒定比例或切换进出Inspector。
Here is my updated code: 这是我更新的代码:
HTML HTML
<div class="horizontal-slide-wrapper">
<h1 class="horizontal-slide">Sliding text</h1>
...
content
...
</div>
CSS CSS
.horizontal-slide-wrapper {
position: relative;
}
.horizontal-slide {
position: fixed;
top: 0;
left: -1000;
}
jQuery jQuery的
$(document).ready(function () {
var $horizontal = $('.horizontal-slide'),
$wrapper = $horizontal.parent(),
$wrapperWidth = $wrapper.width(),
$wrapperHeight = $wrapper.height(),
$wrapperVerticalOffset = $wrapper.offset().top;
$horizontal.css({
'left': 0 - $wrapperVerticalOffset
});
$(window).scroll(function () {
var $scrollPosition = $(this).scrollTop();
var $leftOffset = (($scrollPosition - $wrapperVerticalOffset) * ($wrapperWidth / $wrapperHeight));
$horizontal.css({
'left': $leftOffset
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.