Scroll horizontally starting from right to left with CSS overflow:scroll

后端 未结 6 1853
天命终不由人
天命终不由人 2020-12-16 12:07

So I am wondering if there is a possibility to have a different starting position with the overflow:scroll value;

When you start scrolling in a div

相关标签:
6条回答
  • 2020-12-16 12:28

    You can do this with one line of jQuery:

    $("#box").scrollLeft(480);

    0 讨论(0)
  • 2020-12-16 12:29

    You can of course use direction:rtl

    document.querySelector('input').addEventListener('input', function(){
      document.querySelector('.box').scrollLeft = this.value;
    })
    .box{
      width: 320px;
      height: 100px;
      border:1px solid red;
      overflow: scroll;
      direction: rtl;  /* <-- the trick */
    }
    
    .box::before{ content:''; display:block; width:400%; height:1px; }
    <div class='box'></div>
    <br>
    <input placeholder='scrollLeft value'>

    FiddleDemo

    This may be useful using direction http://css-tricks.com/almanac/properties/d/direction/

    0 讨论(0)
  • 2020-12-16 12:37

    There is an unexpected behavior if you add closing parenthesis at the end of text for the top answer. (Tested in Chrome 54)

    <div id="box">
        <div id="inner_box">
            <div class="item" style="background:red;"></div>
            <div class="item" style="background:green;"></div>
            <div class="item" style="background:blue;">te(st)</div>
        </div>
    </div>
    

    Watch this feedle for result : http://jsfiddle.net/mm37pqxa/

    0 讨论(0)
  • 2020-12-16 12:43

    I've been working in a project and for me works very well like this:

    overflow-x: scroll;   
    overflow-y: hidden;   
    white-space: nowrap;
    
    0 讨论(0)
  • 2020-12-16 12:46

    I don't Know about any solution with just CSS but you can use Jquery to change the initial position of the scrollbar like this:

    $(document).ready(function(){
        $('#box').scrollLeft($(this).height())
    })
    

    Check this Demo Fiddle

    0 讨论(0)
  • 2020-12-16 12:47

    With javascript you can just set scrollLeft property when page gets loaded (using el.scrollLeft = el.scrollWidth - el.clientWidth;).

    0 讨论(0)
提交回复
热议问题