gpt4 book ai didi

jquery - 为什么 jQuery.position() 在 webkit 和 firefox/MSIE 之间表现不同

转载 作者:行者123 更新时间:2023-11-28 19:08:14 25 4
gpt4 key购买 nike

这是一篇很长的文章。我正在尝试在我的网站中实现水平滚动。它在 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);
}

有什么想法吗?我知道我应该知道这个问题的答案,但我一直盯着它看太久了,以至于我对它视而不见。

干杯

最佳答案

我把你的代码弄乱了一点,我认为问题是 position.left 返回对象相对于窗口的位置,它返回滚动条的位置。所以只需将位置变量从 #scrollbar 更改为 #grabber 对我有用。

var position = $("#grabber").position();

因此,您不需要保存 grabberStart 位置

最后,出于某种原因,我花了一段时间才弄清楚,IE 不喜欢绑定(bind)到 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);
}

关于jquery - 为什么 jQuery.position() 在 webkit 和 firefox/MSIE 之间表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2207703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com