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


i have div needs stick right side of page. have accomplished using position:absolute, , setting right , top attributes.

it looks this: img 1

now solution great! today, ran problems. if screen small creates horizontal scrollbar, happen (pay attention scrollbar @ bottom if dont understand problem :) ): img 2 img 3

it sticking right side of window instead of document - okay, unfourtantly move scrollbar, you'll see sticks intially loaded window..

so need div stick right side of document, rather window..

i cannot use fixed. can see solution this? feel lost.

here jsfiddle example

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

a jquery/javascript solution welcome! aslong not massive library :)

you use jquery's .scrollleft() function.

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

attach event listener window scroll event, , adjust div's horizontal position according window's width, , window's horizontal scroll.

something 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


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -