在 jQuery 中将百分比值设置为 scrollLeft()
Set percentage value to scrollLeft() in jQuery
这有点令人困惑(我认为这是不可能的)。我找到了如何从 Blazemonger's answer on SO 获取滚动量的百分比:
scrollPercentage = 100 * $(this).scrollLeft() / ($('#contained').width() - $(this).width());
好的,这个解决方案很有效,但我的问题是如何使用jQuery设置滚动量?
我同时滚动两个可滚动的 div,我试过这个:
$(".mycontainer").scroll(function(){
var scrollPercentage = 100 * $(this).scrollLeft() / ($(".mycontainerContent").width() - $(this).width())
$(".anotherContainer").scrollLeft(scrollPercentage);
});
但该代码不起作用。
我尝试添加百分号,但结果是一样的 - 没有任何反应。
$(".mycontainer").scroll(function(){
var scrollPercentage = 100 * $(this).scrollLeft() / ($(".mycontainerContent").width() - $(this).width())
$(".anotherContainer").scrollLeft(scrollPercentage.toFixed(0)+'%');
});
有什么想法吗?
编辑 - 有点工作:
You need to change percentage value again to original type for scroll
var scrollValueForAnother = ($(".anotherContainer").width() -
$(this).width()) * (percentage / 100);
$(".anotherContainer").scrollLeft(scrollValueForAnother);
I hope that I didn't make a mistake in calculations
但就像我在他的回答的评论中提到的那样:
it's kind of working, but $(".anotherContainer") reaches end before
$(".myContainer") reaches its end. I want to sync two scrollable areas
to reach their end of scroll at the same time. i hope that i could
explain it.
*两个可滚动区域的宽度不同!
您需要再次将百分比值更改为滚动的原始类型
var scrollValueForAnother = ($(".anotherContainer").width() - $(this).width()) * (percentage / 100);
$(".anotherContainer").scrollLeft(scrollValueForAnother);
希望我没有算错
EDIT2:我做了那个小 fiddle https://jsfiddle.net/b4d9bLnm/1/
所以你的代码应该是这样的:
$(".mycontainer").scroll(function(){
var container2 = $(".anotherContainer");
var scrollPercentage = 100 * $(this).scrollLeft() / (this.scrollWidth - this.clientWidth);
var scrollValueForAnother = (container2[0].scrollWidth - container2[0].clientWidth) * (percentage / 100);
$(".anotherContainer").scrollLeft(scrollValueForAnother);
});
这有点令人困惑(我认为这是不可能的)。我找到了如何从 Blazemonger's answer on SO 获取滚动量的百分比:
scrollPercentage = 100 * $(this).scrollLeft() / ($('#contained').width() - $(this).width());
好的,这个解决方案很有效,但我的问题是如何使用jQuery设置滚动量?
我同时滚动两个可滚动的 div,我试过这个:
$(".mycontainer").scroll(function(){
var scrollPercentage = 100 * $(this).scrollLeft() / ($(".mycontainerContent").width() - $(this).width())
$(".anotherContainer").scrollLeft(scrollPercentage);
});
但该代码不起作用。
我尝试添加百分号,但结果是一样的 - 没有任何反应。
$(".mycontainer").scroll(function(){
var scrollPercentage = 100 * $(this).scrollLeft() / ($(".mycontainerContent").width() - $(this).width())
$(".anotherContainer").scrollLeft(scrollPercentage.toFixed(0)+'%');
});
有什么想法吗?
编辑 -
You need to change percentage value again to original type for scroll
var scrollValueForAnother = ($(".anotherContainer").width() - $(this).width()) * (percentage / 100); $(".anotherContainer").scrollLeft(scrollValueForAnother);
I hope that I didn't make a mistake in calculations
但就像我在他的回答的评论中提到的那样:
it's kind of working, but $(".anotherContainer") reaches end before $(".myContainer") reaches its end. I want to sync two scrollable areas to reach their end of scroll at the same time. i hope that i could explain it.
*两个可滚动区域的宽度不同!
您需要再次将百分比值更改为滚动的原始类型
var scrollValueForAnother = ($(".anotherContainer").width() - $(this).width()) * (percentage / 100);
$(".anotherContainer").scrollLeft(scrollValueForAnother);
希望我没有算错
EDIT2:我做了那个小 fiddle https://jsfiddle.net/b4d9bLnm/1/ 所以你的代码应该是这样的:
$(".mycontainer").scroll(function(){
var container2 = $(".anotherContainer");
var scrollPercentage = 100 * $(this).scrollLeft() / (this.scrollWidth - this.clientWidth);
var scrollValueForAnother = (container2[0].scrollWidth - container2[0].clientWidth) * (percentage / 100);
$(".anotherContainer").scrollLeft(scrollValueForAnother);
});