【问题标题】:Why is jQuery.position() behaving differently between webkit and firefox/MSIE为什么 jQuery.position() 在 webkit 和 firefox/MSIE 之间表现不同
【发布时间】:2011-01-13 13:01:33
【问题描述】:

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

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

干杯

【问题讨论】:

    标签: jquery css firefox


    【解决方案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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      相关资源
      最近更新 更多