我正在尝试实现带有标题横幅的滑动水平布局。
这是 HTML 结构:
<body>
<div id="header">
<div><a href="#one"> one </a></div>
<div><a href="#two"> two </a></div>
<div><a href="#thr"> thr </a></div>
</div>
<div id="one" class="panel"> </div>
<div id="two" class="panel"> </div>
<div id="thr" class="panel"> </div>
</body>
标题是固定的,面板已经提供了渐变背景(中间面板有不同的颜色用于调试目的)。 这是 CSS:
body {
width: 6000px;
overflow: hidden;
}
.panel {
width: 33.3%;
float: left;
padding-left: 30px;
padding-right: 1040px;
margin-top: -75px;
height: 960px;
background-image: linear-gradient(to bottom, #0B88FF, #95EEFF);
}
#header {
position: fixed;
height: 75px;
margin-top: 25px;
}
#two{
background-image: linear-gradient(to bottom, #0B8800, #9E5EFF);
}
最后是管理面板间动画的函数:
$(document).ready(function() {
$("#header a").bind("click", function(event) {
event.preventDefault();
var target = $(this).attr("href");
$("html, body").stop().animate({
scrollLeft: $(target).offset().left,
scrollTop: $(target).offset().top
}, 1200);
});
});
我面临的问题如下:
1) 我尝试实现一个 jQuery 函数来在用户使用鼠标滚轮时运行幻灯片动画,但是我的测试都不起作用...结构始终相同:
$(window).scroll(function() {
if ($(this).scrollTop() > 0) {
var target // still not able to figure out who should i target
$("html, body").stop().animate({
//to the target >,<
}
});
2) 当我的浏览器窗口大小为 100% 时,一切似乎都运行良好,但如果我减少或增加缩放比例,一切都会变得困惑 >,< 我注意到可以处理这个,并且 here is an example:
最佳答案
要获得您的目标,您只需使用 panel
类填充一个包含元素的数组,然后使用索引在面板中移动。
最后,如果您在调整窗口大小时遇到滚动问题,您可以绑定(bind)此 event随心所欲
看看MouseWheelEvent .
然后用你的代码试试这个例子:
$(document).ready(function() {
$("#header a").bind("click", function(event) {
event.preventDefault();
var target = $(this).attr("href");
$("html, body").stop().animate({
scrollLeft: $(target).offset().left,
scrollTop: $(target).offset().top
}, 1200);
});
var scroll_targets = $(".panel");
var scroll_targets_index = 0;
$(window).on('DOMMouseScroll mousewheel', function (e) {
if (e.originalEvent.wheelDelta < 0) {
if(scroll_targets_index < scroll_targets.length-1){
var where = ++scroll_targets_index;
$("html, body").stop().animate({
scrollLeft: $(scroll_targets[where]).offset().left,
scrollTop: $(scroll_targets[where]).offset().top
}, 1200);
}
}
else {
var where;
if(scroll_targets_index > 0){
where = --scroll_targets_index;
}
else{
where = 0;
}
$("html, body").stop().animate({
scrollLeft: $(scroll_targets[where]).offset().left,
scrollTop: $(scroll_targets[where]).offset().top
}, 1200);
}
});
$(window).resize(function () {
$('html,body').scrollTop($(scroll_targets[where]).offset().top);
$('html,body').scrollLeft($(scroll_targets[where]).offset().left);
});
});
#body {
width: 6000px;
overflow: hidden;
}
.panel {
width: 33.3%;
float: left;
padding-left: 30px;
padding-right: 1040px;
margin-top: -75px;
height: 960px;
background-image: linear-gradient(to bottom, #0B88FF, #95EEFF);
}
#header {
position: fixed;
height: 75px;
margin-top: 25px;
}
#two{
background-image: linear-gradient(to bottom, #0B8800, #9E5EFF);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="body">
<div id="header">
<div><a href="#one"> one </a></div>
<div><a href="#two"> two </a></div>
<div><a href="#thr"> thr </a></div>
</div>
<div id="one" class="panel"> </div>
<div id="two" class="panel"> </div>
<div id="thr" class="panel"> </div>
</div>
关于jquery - 滑动水平响应式布局和鼠标滚轮输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35572905/