0

I have a button I need to position at the bottom of the page in the right side.

https://jsfiddle.net/zuk80g6L/

button {
    position: fixed;
    right: 16px;
    bottom: 16px;
}

That button should move further into the screen (like right: 24px if we assume the scrollbar is 8px wide), when the scrollbar is visible. Right now it just pretty much says the total view is 1920 pixels wide. The button should always be 16 pixels from the right side.

I need to take this into account:

  1. Is the scrollbar visible
  2. What's the width of the scrollbar
  3. What if there is a scrollbar, but it isn't changing the site (like on a phone)
  4. It has to be fixed

Basically if there is a scrollbar, move the button further left onto the site.

How is something like that possible? Thanks

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116
  • Not certain I understand.. if the *browser window* adds the scrollbar automatically, then content is customarily also *automatically* moved left to compensate. Are you speaking of the browser window scrollbar or a scrollbar created due to overflow properties? – Scott Aug 26 '16 at 09:03
  • @Scott If the browser window adds the scrollbar, the button does not move. Why? Because the button is fixed. It doesn't take the scrollbar into account. – MortenMoulder Aug 26 '16 at 09:05
  • 1
    [The button moves here](http://i.stack.imgur.com/BXSlg.gif) when the scroll becomes necessary in your fiddle.... You may have something else causing the issue you are seeing. – Scott Aug 26 '16 at 09:06
  • @Scott I see... I must've missed that. Then something else is not making my button move... hmm. – MortenMoulder Aug 26 '16 at 09:11
  • Maybe fixed widths containers with overflowX hidden??? – Scott Aug 26 '16 at 10:13
  • @Scott Not possible. I'm using Angular and it's very responsive on all platforms right now, so I'd rather not mess with it. – MortenMoulder Aug 26 '16 at 10:36
  • I meant perhaps that is the problem, not that you should implement such things. – Scott Aug 26 '16 at 11:23

2 Answers2

0

Use these solutions to get if scrollbar is presented:

  1. How can I check if a scrollbar is visible?
    1. Detect if a page has a vertical scrollbar?

Based on that trigger the class/css for the button, like

if (hasScrollbar) {
  /* Move button right for 8px */
}

In addition, you can get scrollbar width

Community
  • 1
  • 1
Mihey Egoroff
  • 1,542
  • 14
  • 23
0

I have done some modifications on you code.

First I added a div that i used as a container and gave it Id #content

to check if the scroll bar is added to the page check the height of the content div and the window height. if the window height is smaller then the scroll bar is added and we give the button a right 24px;

here is the js script that does the job

var contenth = $("#content").height();
var windowh = $(window).height();
if  (contenth>windowh){
$("#bottom_button").css('right','24px');
}

and the updated jsfiddle

P.S it cannot be made without css.

Zenel Rrushi
  • 2,346
  • 1
  • 18
  • 34