这是一个很长的问题。我正在尝试在我的网站中实现水平滚动。它在Safari和Chrome中运行良好,但在Firefox中却不行(我还没有开始讨论IE的问题)。
据我所知,Webkit正在使用滚动条抓取器div的相对位置,而firefox正在使用它相对于文档的位置。
You can test it here了解发生了什么。
这是滚动条的CSS
/* The Scrollbar */
#scrollbar
{
position: relative;
width: 70%;
display: block;
margin: 0px auto;
border: #444444 1px solid;
border-width: 0 0 1px 0;
overflow: visible;
}
#grabber
{
position: relative;
top: 8px;
left: 0px;
height: 20px;
display: block;
background: url(assets/grabber-left.png) no-repeat;
}
#grabber-end
{
height: inherit;
width: 50%;
background: url(assets/grabber-right.png) no-repeat;
background-position: 100% 0;
float: right;
}
支持它的jQuery
var grabberClicked = false;
var dragStart;
var grabberStart;
var ratio;
var scrollBound;
var totalWidth = 0;
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
$(document).ready(function(){
$("#projects").width(getTotalWidth());
setup();
$("#grabber").mousedown(startScroll);
$(window).mouseup(endScroll);
$("#viewport").scroll(positionGrabber);
$(window).resize(setup);
});
function getTotalWidth(){
$(".project").each(function(){
totalWidth += $(this).width();
totalWidth += parseInt($(this).css("marginLeft")) * 2;
})
return totalWidth;
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function setup(){
ratio = $("#viewport").width() / $("#projects").width();
// grabber width
$("#grabber").width( $("#scrollbar").width() * ratio );
scrollBound = $("#scrollbar").width() - $("#grabber").width();
// incase the user resizes the window, position the grabber accordingly
positionGrabber();
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function startScroll(event){
$(window).bind('mousemove', scroll);
var position = $("#scrollbar").position();
dragStart = event.pageX - position.left;
grabberStart = parseInt($("#grabber").css("left"));
$("#feedback").html($("#grabber").position().left);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function endScroll(event){
$(window).unbind('mousemove', scroll);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function scroll(event){
var newPos = grabberStart + (event.pageX - dragStart);
//$("#feedback").html($("#grabber").position().left +" // "+ newPos);
// bounds
newPos = (newPos > 0) ? newPos : 0;
newPos = (newPos < scrollBound) ? newPos : scrollBound;
viewportPos = ( $("#projects").width() * ( newPos / $("#scrollbar").width() ) );
$("#viewport").scrollLeft(viewportPos);
$("#grabber").css("left", newPos);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function positionGrabber(event){
var grabberPos = $("#scrollbar").width() * ($("#viewport").scrollLeft() / $("#projects").width());
$("#grabber").css("left", grabberPos);
}
有什么想法吗?我知道我应该知道这个问题的答案,但是我一直盯着它,所以我对它视而不见。
欢呼声
答案 0 :(得分:1)
我用你的代码搞砸了一下,我认为问题是position.left
返回对象相对于窗口的位置,它返回滚动条位置。所以只需将位置变量从#scrollbar
更改为#grabber
就可以了。
var position = $("#grabber").position();
因此,您无需保存grabberStart
职位
最后,出于某种原因,我花了一段时间才弄清楚,IE不喜欢绑定到window
事件。所以我把它们改为document
和BAM! IE工作正常。
这是您使用我提到的更改更新的脚本。顺便看看好看的网站!
// **********************************************
// Throll: Toms Horizontal Scroll
// Developer: Tom Elders
// Contact: him@tomelders.com
// **********************************************
// File: throll.1.0.js
// Description:
// CSS and JS horizontal scriolling that doesn't
// break the browers native functionality.
//
// Copyright 2010, Tom Elders
//
// **********************************************
var grabberClicked = false;
var dragStart;
var ratio;
var scrollBound;
var totalWidth = 0;
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
$(document).ready(function(){
$("#projects").width(getTotalWidth());
setup();
$("#grabber").mousedown(startScroll);
$(document).mouseup(endScroll);
$("#viewport").scroll(positionGrabber);
$(window).resize(setup);
});
function getTotalWidth(){
$(".project").each(function(){
totalWidth += $(this).width();
totalWidth += parseInt($(this).css("marginLeft")) * 2;
})
return totalWidth;
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function setup(){
ratio = $("#viewport").width() / $("#projects").width();
// grabber width
$("#grabber").width( $("#scrollbar").width() * ratio );
scrollBound = $("#scrollbar").width() - $("#grabber").width();
// incase the user resizes the window, position the grabber accordingly
positionGrabber();
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function startScroll(event){
$(document).bind('mousemove', scroll);
var position = $("#grabber").position();
dragStart = event.pageX - position.left;
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function endScroll(event){
$(document).unbind('mousemove', scroll);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function scroll(event){
var newPos = event.pageX - dragStart;
// bounds
newPos = (newPos > 0) ? newPos : 0;
newPos = (newPos < scrollBound) ? newPos : scrollBound;
viewportPos = ( $("#projects").width() * ( newPos / $("#scrollbar").width() ) );
$("#viewport").scrollLeft(viewportPos);
$("#grabber").css("left", newPos);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function positionGrabber(event){
var grabberPos = $("#scrollbar").width() * ($("#viewport").scrollLeft() / $("#projects").width());
$("#grabber").css("left", grabberPos);
}