24

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 the default behaviour is to scroll from left to right:

|<--Scrollbar-Starting-Left-->_________________________________________________|

would it possible that it starts at the right?

|_________________________________________________<--Scrollbar-Starting-Right-->|

In my example the red and green items are first visible, I'd like the green and blue item to be visible first :)

I've tried direction:rtl; but no luck

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
MMachinegun
  • 3,044
  • 2
  • 27
  • 44

7 Answers7

43

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/

vsync
  • 118,978
  • 58
  • 307
  • 400
Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
7

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

DaniP
  • 37,813
  • 8
  • 65
  • 74
3

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

overflow-x: scroll;   
overflow-y: hidden;   
white-space: nowrap;
Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
2

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

vbo
  • 13,583
  • 1
  • 25
  • 33
1

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/

  • It is unclear what the unexpected behavior is. Please elaborate and explain what your answer adds over and above the existing answers. How does this answer address the OP's question? – Brian D Dec 06 '16 at 14:50
  • your demonstrated fiddle has an issue even if you center your text or align it to the left. The closing bracket is cut off either way, so I don't think this is an issue with the scrolling itself, it's definitively an other issue unrelated to the scrolling. Cheers – MMachinegun Dec 06 '16 at 16:25
  • Okay, never mind my previous answer. You are right, `direction:rtl;` does change the alignment of some characters. I think it's symbols like `?`, `!` and `.` Somehow the bracket falls into this as well. I guess this has to do with the direction text normally flows when being red in a rtl-language. Your fix to this would be to assign `direction: ltr;` to your child-element to reinstate the "normal" right-to-left flow again. [fiddle](http://jsfiddle.net/zztp36u3/) – MMachinegun Dec 06 '16 at 16:36
0

You can do this with one line of jQuery:

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

ElendilTheTall
  • 1,344
  • 15
  • 23
-1

Use direction rtl

overflow: auto; 
direction:rtl; 
Pankaj Upadhyay
  • 2,114
  • 21
  • 22