如何控制页面所有滚动条随着一个滚动条一起动
1、项目开发需要去查看页面上的四幅图片,而且需要同时查看这些图片的相同的相对位置来进行比对,因此想实现滚动图片1的滚动条时,图片2伴随一起运动。
2、
3、图片效果如图,如何实现呢?
回答:
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body{padding: 100px;}
*{margin: 0;padding: 0;box-sizing:border-box;}
div{width: 200px;height: 300px;overflow: scroll;border: 1px solid green;float: left;}
</style>
</head>
<body>
<div class="box1">
<p>h ivherhgeiah ilaherli ghesr lhcfknrwerovwerve;irvehkrvhweoytvowehfvhe fhriweuhf ciuwegfiwegfihawhrovyitybiqyrtiviweroweyvahiwoetuweyricworiwjo wayiyawvbiahwefhcwehacaiyeifhaekhfvkshlkfvhlsehilseyylsvhsliuhiuyriehtvlbaherflhlvh ivherhgeiah ilaherli ghesr lhcfknrwerovwerve;irvehkrvhweoytvowehfvhe fhriweuhf ciuwegfiwegfihawhrovyitybiqyrtiviweroweyvahiwoetuweyricworiwjo wayiyawvbiahwefhcwehacaiyeifhaekhfvkshlkfvhlsehilseyylsvhsliuhiuyriehtvlbaherflhlvh ivherhgeiah ilaherli ghesr lhcfknrwerovwerve;irvehkrvhweoytvowehfvhe fhriweuhf ciuwegfiwegfihawhrovyitybiqyrtiviweroweyvahiwoetuweyricworiwjo wayiyawvbiahwefhcwehacaiyeifhaekhfvkshlkfvhlsehilseyylsvhsliuhiuyriehtvlbaherflhlvh ivherhgeiah ilaherli ghesr lhcfknrwerovwerve;irvehkrvhweoytvowehfvhe fhriweuhf ciuwegfiwegfihawhrovyitybiqyrtiviweroweyvahiwoetuweyricworiwjo wayiyawvbiahwefhcwehacaiyeifhaekhfvkshlkfvhlsehilseyylsvhsliuhiuyriehtvlbaherflhlvh ivherhgeiah ilaherli ghesr lhcfknrwerovwerve;irvehkrvhweoytvowehfvhe fhriweuhf ciuwegfiwegfihawhrovyitybiqyrtiviweroweyvahiwoetuweyricworiwjo wayiyawvbiahwefhcwehacaiyeifhaekhfvkshlkfvhlsehilseyylsvhsliuhiuyriehtvlbaherflhlv</p>
</div>
<div class="box2">
<p>h ivherhgeiah ilaherli ghesr lhcfknrwerovwerve;irvehkrvhweoytvowehfvhe fhriweuhf ciuwegfiwegfihawhrovyitybiqyrtiviweroweyvahiwoetuweyricworiwjo wayiyawvbiahwefhcwehacaiyeifhaekhfvkshlkfvhlsehilseyylsvhsliuhiuyriehtvlbaherflhlvh ivherhgeiah ilaherli ghesr lhcfknrwerovwerve;irvehkrvhweoytvowehfvhe fhriweuhf ciuwegfiwegfihawhrovyitybiqyrtiviweroweyvahiwoetuweyricworiwjo wayiyawvbiahwefhcwehacaiyeifhaekhfvkshlkfvhlsehilseyylsvhsliuhiuyriehtvlbaherflhlvh ivherhgeiah ilaherli ghesr lhcfknrwerovwerve;irvehkrvhweoytvowehfvhe fhriweuhf ciuwegfiwegfihawhrovyitybiqyrtiviweroweyvahiwoetuweyricworiwjo wayiyawvbiahwefhcwehacaiyeifhaekhfvkshlkfvhlsehilseyylsvhsliuhiuyriehtvlbaherflhlvh ivherhgeiah ilaherli ghesr lhcfknrwerovwerve;irvehkrvhweoytvowehfvhe fhriweuhf ciuwegfiwegfihawhrovyitybiqyrtiviweroweyvahiwoetuweyricworiwjo wayiyawvbiahwefhcwehacaiyeifhaekhfvkshlkfvhlsehilseyylsvhsliuhiuyriehtvlbaherflhlvh ivherhgeiah ilaherli ghesr lhcfknrwerovwerve;irvehkrvhweoytvowehfvhe fhriweuhf ciuwegfiwegfihawhrovyitybiqyrtiviweroweyvahiwoetuweyricworiwjo wayiyawvbiahwefhcwehacaiyeifhaekhfvkshlkfvhlsehilseyylsvhsliuhiuyriehtvlbaherflhlv</p>
</div>
</body>
<script src="E:/作业/914/京东/js/jquery-3.1.0.min.js"></script>
<script>
//方法1
$('.box1').scroll(function(){
var a=$(this).scrollTop();
var b=$(this).scrollLeft();
$('.box2').
animate({'scrollTop':a,'scrollLeft':b},0)
})
$('.box2').scroll(function(){
var a=$(this).scrollTop();
var b=$(this).scrollLeft();
$('.box1').
animate({'scrollTop':a,'scrollLeft':b},0)
})
//方法2
$('div').scroll(function(){
var a=$(this).scrollTop();
var b=$(this).scrollLeft();
$('div').animate({'scrollTop':a,'scrollLeft':b},0);
})
</script>
</html>
回答:
监听所有图片容器 div
的 onscroll
事件
修改其他 div
的 scrollTop
为当前事件触发元素的 scrollTop
回答:
那个,没做过类似的项目,找了一个方法,获取滚动距离的,你可以用js来动态修改图2 的滚动距离:
function ___getPageScroll() {
var xScroll, yScroll;
if (self.pageYOffset) {
yScroll = self.pageYOffset;xScroll = self.pageXOffset;
} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
yScroll = document.documentElement.scrollTop;xScroll = document.documentElement.scrollLeft;
} else if (document.body) {// all other Explorers
yScroll = document.body.scrollTop;xScroll = document.body.scrollLeft;
}
arrayPageScroll = new Array(xScroll,yScroll);
return arrayPageScroll;
};
以上是 如何控制页面所有滚动条随着一个滚动条一起动 的全部内容, 来源链接: utcz.com/p/189356.html