简体   繁体   English

元素滑块从0开始到底部(负)而不是在Firefox中从0到顶部

[英]element slider start from 0 to bottom (negative) instead 0 to top in firefox

The element slider in all browsers starts from 0 up to top (ex: 0 to 500 ) that's when the direction of page is left to right , and when the direction of page is right to left the slider starts from top to 0 (ex: 500 to 0 ). 所有浏览器中的元素滑块从0到顶部(例如: 0 to 500 )从页面方向left to right ,当页面方向right to left ,滑块从顶部开始到0(例如: 500 to 0 )。
The firefox is the only browser which is different when the direction of page is right to left starts from 0 to bottom (ex: 0 to -500 ) negative number. 当页面right to left从0到底(从0 to -500 )负数开始时, firefox是唯一不同的浏览器。

 var box = document.getElementById('box'); var result = document.getElementById('result'); box.onscroll = function() { result.innerHTML = box.scrollLeft; } 
 body { direction: rtl; } #box { width: 500px; height: 250px; margin: 0 auto; overflow-y: hidden; overflow-x: scroll; } #childBox { width: 1000px; height: 250px; background: url('http://images.all-free-download.com/images/graphiclarge/school_symbols_seamless_pattern_311368.jpg') repeat-x; overflow: hidden; } 
 <div id="box"> <div id="childBox"></div> </div> <div id="result"></div> 

Also jsfiddle . 也是jsfiddle

How to make the firefox to treats the element slider like all browsers ? 如何使firefox像所有浏览器一样处理元素滑块?

Now that's something I didn't know, cool. 现在这是我不知道的事情,很酷。 Even the MDN explicitly says that Firefox' is the correct behavior . 甚至MDN明确表示Firefox'是正确的行为 But if all the other browser behaves like that, there's no point in being different, especially for an old spec like scrollLeft . 但是,如果所有其他浏览器都表现得那样,那就没有什么不同了,特别是对于像scrollLeft这样的旧规范。

Anyway, there is a way to make Firefox work like the other ones, and involves a (rather invasive, to be fair) trick. 无论如何,有一种方法可以让Firefox工作像其他的人,和涉及(而创的,是公平的)技巧。 Firefox also supports the non-standard property scrollLeftMax which may come in handy, because it saves us to check the element's computed style: if an element is horizontally scrollable, but scrollLeftMax is zero, then it's right-to-left and needs to be adjusted: Firefox还支持非标准属性scrollLeftMax ,它可以派上用场,因为它可以节省我们检查元素的计算样式:如果一个元素是水平可滚动的,但scrollLeftMax是零,那么它是从右到左需要调整:

if (!("scrollLeftMax" in Element.prototype)) return;

var scrollLeftDesc = Object.getOwnPropertyDescriptor(Element.prototype, "scrollLeft");

Object.defineProperty(Element.prototype, "scrollLeft", {
    get: function() {
        var diff = this.scrollWidth - this.clientWidth;

        return scrollLeftDesc.get.call(this) + (this.scrollLeftMax ? 0 : diff);
    },
    set: function(v) {
        var diff = this.scrollWidth - this.clientWidth;

        scrollLeftDesc.set.call(this, v - (this.scrollLeftMax ? 0 : diff));

        return v;
    }
});

The scroll method should be taken care of too if needed or for completeness: 如果需要或完整性,也应该注意scroll方法:

var originalScroll = Element.prototype.scroll;

Element.prototype.scroll = function(x, y) {
    var diff = this.scrollWidth - this.clientWidth;

    originalScroll(x - (this.scrollLeftMax ? 0 : diff), y);
};

The point is understanding when to apply it. 关键是要了解何时应用它。 I don't think there's a way to preemptively check this without adding a dummy element to the DOM. 我认为没有一种方法可以在不向DOM添加虚拟元素的情况下预先检查这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM