简体   繁体   中英

HTML/CSS: Using position absolute to stick to the side, when there is a horizontal scrollbar

I have a div that needs to stick to the right side of the page. I have accomplished this using position:absolute , and setting the right and top attributes.

It looks something like this: 图1

Now this solution is great! But today, we ran into some problems. If the screen is so small that it creates a horizontal scrollbar, this will happen (Pay attention to the scrollbar at the bottom if you dont understand the problem :) ): 图2图3

It is sticking to the right side of the window instead of the document - which would actually be okay, but unfourtantly as soon as you move the scrollbar, you'll see that it only sticks to the intially loaded window..

So I need the div to stick to the right side of the document, rather than the window..

I cannot use fixed. Can anyone see a solution for this? I feel lost.

Here is an Jsfiddle Example

<div style="background-color:red;
width:50px;
height:100px;
position:absolute;
top:20px;
right:0%;">
    test
</div>

A jQuery/JavaScript solution is welcome! Aslong as its not a massive library :)

use one container div with

{width:100%; position:absolute; top:50px; left:0px; }

in that div put your div and set

{float:right; width:80px; height:300px;}

ng Thomas

You could use Jquery's .scrollLeft() function.

http://api.jquery.com/scrollleft/

Attach an event listener to the window scroll event, and adjust the div's horizontal position according to the window's width, and window's horizontal scroll.

Something like this:

$(window).on("scroll", function() {
    var vW = $(window).width();
    var vS = $(window).scrollLeft();
    var dvW = $("#dv1").width();

    $("#dv1").css("left", vW - dvW + vS);
});

Updated working fiddle

Wow, a simple thing made complicated. The horizontal scroll bars kick in, not because of the absolute positioned element, but because the padding or the margin you're using on the content to not cover itself when the viewport is smaller.

This is corrected by using box-sizing:border-box on the content and using padding, not margin to make a space for it.

DEMO: http://jsbin.com/wulih/1/

EDIT: http://jsbin.com/wulih/1/edit

DEMO CSS:

html,
body {
    padding: 0;
    margin: 0;
    background: #fff;
    font-family: sans-serif;
    line-height: 1.6;
}
.red {
    background-color: red;
    width: 50px;
    height: 600px;
    position: absolute;
    top: 20px;
    right: 0;
}
.content {
    padding: 0 70px 20px 20px;
    box-sizing: border-box;
}

DEMO HTML:

<div class="red"></div>
<div class="content">
   <h1>HTML Ipsum Presents</h1>
   <p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>
   <h2>Header Level 2</h2>
   <ol>
      <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
      <li>Aliquam tincidunt mauris eu risus.</li>
   </ol>
   <blockquote>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.</p>
   </blockquote>
   <h3>Header Level 3</h3>
   <ul>
      <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
      <li>Aliquam tincidunt mauris eu risus.</li>
   </ul>
   <pre><code>
#header h1 a { 
    display: block; 
    width: 300px; 
    height: 80px; 
}
</code></pre>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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