bootstrap-modal icon indicating copy to clipboard operation
bootstrap-modal copied to clipboard

Opening a new modal causes page to scroll top

Open lmartins opened this issue 11 years ago • 59 comments

This was solved on a old Bootstrap issue (https://github.com/twitter/bootstrap/issues/5336) but still persists on this plugin.

The issue is, when you open a modal from a long page, the page will scroll to the top and then opens the modal. When the modal is closed the pages stays at top.

Thanks.

lmartins avatar Jun 13 '13 15:06 lmartins

I was able to solve this by removing line 98 from bootstrap-modalmanager.js: that.setFocus();

Everything still seemed to work after removing this, and the page no longer scrolls in Chrome or Safari. Not sure what the unforeseen consequences are though...

henrythe9th avatar Jul 14 '13 23:07 henrythe9th

got the same problem

nediam avatar Sep 23 '13 18:09 nediam

if you are using rails and the asset pipeline, make sure the bootstrap-modal from this repository is included. i was using a bootstrap from a gem and /assets/bootstrap-modal.js got mounted by that gem. an easy fix is, to put the js files from this repo into a subfolder of your assets folder

durandom avatar Oct 18 '13 13:10 durandom

Ok, the issue is that the plugin is setting the focus on the modal, while the modal is still pulled away from the viewport. I've basically removed the slide-down animation, but a more definitive solution will probably involve changing the timing for the focus to happen.

lmartins avatar Oct 19 '13 22:10 lmartins

I'm running into the same problem. Disabling animation by way of removing the fade class doesn't seem to fix the issue for me. Increasing emulateTransitionEnd() from 300 to 10000 doesn't seem to help either.

johnwp avatar Jan 10 '14 18:01 johnwp

got the same problem +1

ivawzh avatar Feb 17 '14 04:02 ivawzh

I think this is because your href is set to "#". Changing this fixed it for me.

cpuguy83 avatar Feb 18 '14 18:02 cpuguy83

Mine is a tag without href attribute. It just doesn't fix anything.

ivawzh avatar Feb 18 '14 21:02 ivawzh

I have the same problem too.

taenaive avatar Feb 27 '14 15:02 taenaive

Anyone able to create a jsfiddle showing the issue? I have yet to encounter this problem.

jschr avatar Feb 27 '14 16:02 jschr

I have this issue only on mobile. Try the modal on iphone 4 or original Ipad.

http://getbootstrap.com/javascript/#modals Clicking the "launch demo modal" causes the page to scroll to the top.

taylorhakes avatar Mar 11 '14 05:03 taylorhakes

So the example on the bootstrap page doesn't scroll to top but mine does... http://getbootstrap.com/javascript/#modals

I've copied their code exactly and it still happens with my version using the same bootstrap.min.js v3.1.1

I can't replicate with jsfiddle, it stays in place too, http://jsfiddle.net/U7LUR/

This leads me to think it has something to with DOM structure, css and containers.

Removing fade didn't work, removing href="#"/data-target="#" didn't work. I can't remove that.setFocus(); as it doesn't seem to be in bootstrap.js.

A solution would be great as I really like the modal boxes but this problem is going to prevent the project going ahead unless resolved.

TobyTobias avatar Apr 24 '14 06:04 TobyTobias

@TobyTobias you are referencing the default bootstrap modal. The docs for this plugin can be found at https://github.com/jschr/bootstrap-modal. I am unclear about whether you are actually using this plugin or not.

jschr avatar Apr 24 '14 12:04 jschr

@jschr my mistake, sorry.

TobyTobias avatar Apr 29 '14 03:04 TobyTobias

As I mentioned here https://github.com/jschr/bootstrap-modal/issues/131#issuecomment-26595552 this is something most people run into, that report this bug. Maybe renaming bootstrap-modal would help?

durandom avatar Apr 29 '14 06:04 durandom

cpuguy83 solved my problem. Thanks!

penguinnorth avatar Sep 30 '14 17:09 penguinnorth

@cpuguy83...Thank u...changing href fixed it for me!

KrutiRaghavan avatar Nov 07 '14 23:11 KrutiRaghavan

change your href="#" to href="#responsive" visit our website rspate.com

rudrarakeshpaul avatar Jun 02 '15 23:06 rudrarakeshpaul

change href="#" to href="javascript:;"

kiranpatil353 avatar Aug 03 '15 09:08 kiranpatil353

Hello guys, when I first open a modal, it jumps to the top of my page. Is there a fix to avoid this behaviour?

Best,

billyshena avatar Aug 06 '15 18:08 billyshena

Still problem is not solved.

ChameleonRed avatar Sep 09 '15 09:09 ChameleonRed

I had the same problem and I realized, it was because of html{ overflow: scroll; } in my css. I hope you find this useful.

miguelmich avatar Sep 09 '15 12:09 miguelmich

I have the same issue

damianpawski avatar Sep 09 '15 12:09 damianpawski

I have the same issue but only in IE10 - 11, in some case it was scrolling the page to the bottom. Adding position fixed to body.modal-open made it scroll to the top instead but I can't find any solution to make it works properly in IE.

I narrowed to the point where I think it is a conflict with another JS that is included but I am in a AngularJS project and everything is JS.

Tried the normal Angular Bootstrap stack in a simple project and everything works fine even in IE.

At this point I am little out of resources, spent 3 days trying to debug that thing. Hope someone will have a working solution.

jcviau81 avatar Sep 22 '15 15:09 jcviau81

Is there fix for this problem? I am facing the same issue.

shreyharia avatar Oct 22 '15 17:10 shreyharia

I did not found a fix to the problem but I did found a workaround.

What I did is get the current scroll position and re-apply the scroll position when I call the modal.open() with a timeout off 300ms to match the default modal animation duration. Kind of a hack more than a fix but working fine.

jcviau81 avatar Oct 22 '15 18:10 jcviau81

+1

Same issue... when you try to prevent the background/body from scrolling by the usual: body.modal-open { position:fixed; width:100%; } It automatically scrolls you to the Top...

I'll Post a fix if I can ever figure this out.

MarkPieszak avatar Oct 23 '15 06:10 MarkPieszak

Adding this rule to my css fixed it globally for me:

body.modal-open {
    overflow: visible;
}

See this Stack Overflow post: http://stackoverflow.com/questions/21604674/bootstrap-modal-background-jumps-to-top-on-toggle

I have not confirmed this, but according to some of the comments, it was necessary in my case because I have body { height: 100%; } specified.

GaleForceVR avatar Nov 03 '15 16:11 GaleForceVR

@GaleForceVR comment have helped here, thanks. :+1:

giovannipds avatar Nov 12 '15 04:11 giovannipds

+1

Same

alfijuan avatar Jan 21 '16 13:01 alfijuan

change your html "" to "

" or "" and remove "href" attribute ..

check new bootstrap modal appear style..

http://rspate.com/2016/01/bootstrap-modal-display-in-new-style/

On Thu, Jan 21, 2016 at 6:43 PM, Juan Alfieri [email protected] wrote:

+1

Same

— Reply to this email directly or view it on GitHub https://github.com/jschr/bootstrap-modal/issues/131#issuecomment-173564616 .

rudrarakeshpaul avatar Jan 24 '16 05:01 rudrarakeshpaul

didn't work, but thnks

alfijuan avatar Jan 27 '16 13:01 alfijuan

I'm using the Angular Material module in the same app and noticed angular material sets CSS on the <body> and <html> to height:100% and as @GaleForceVR mentioned, this height setting was in fact the problem.

stephengardner avatar Feb 27 '16 06:02 stephengardner

In my case, I changed .modal-open{ position: relative} (it was "fixed" before) and it works.

freeecoom avatar Mar 02 '16 15:03 freeecoom

.modal-open{ position: relative; }

This method really work!

ThanawatMas avatar Apr 28 '16 06:04 ThanawatMas

#miguelmich thanks, i got the issue.

davinder-kumar avatar Jun 10 '16 05:06 davinder-kumar

A developer friend of mine told me to substitute href="#" with href="javascript: return false" (similar to what @kiranpatil353 and @cpuguy83 suggested). Guess what.... it worked! Hope this could help.

umbe1987 avatar Jun 10 '16 14:06 umbe1987

When the bootstrap modal opens then .modal-open class is set to body tag. In this tag overflow:hidden is set. we have to change this. Add the below code in your CSS.

Reference :- www.latestphptutorials.com/?p=437

sudhanshu077 avatar Sep 05 '16 11:09 sudhanshu077

.modal-open { overflow: visible !important; }

worked for me

ricky300300 avatar Oct 12 '16 21:10 ricky300300

I have solved this issue.

Please use following code:

var scrollPos = 0; $('.modal') .on('show.bs.modal', function (){ scrollPos = $('body').scrollTop(); $('body').css({ overflow: 'hidden', position: 'fixed', top : -scrollPos }); }) .on('hide.bs.modal', function (){ $('body').css({ overflow: '', position: '', top: '' }).scrollTop(scrollPos); });

jeetendraj2006 avatar Dec 19 '16 06:12 jeetendraj2006

Here is what I had: href="#" and here is what fixed it for me: href="javascript:;"

Before I figured this out, I was using a javascript work around. I hope it is useful to someone.

$(".ApproveButton").click(function () { var scroll = $(document).scrollTop(); $('#ApproveModal').modal({ backdrop: 'static', keyboard: false }); $(document).scrollTop(scroll); });

When I clicked the button the page still jumped to the top but went right back down. Not the prettiest solution but the app was not public facing and the users didn't seem to care. The users just cared that they lost their scroll position.

rev319303 avatar Feb 28 '17 13:02 rev319303

I basically solved the problem here. Open the modal before the record scroll position. Open modal after the location will be just returned to the scroll. The display does not have any effect on the page

var default_scrollTop = 0; $('.modal').on('show.bs.modal', function () { default_scrollTop = document.body.scrollTop; }); $('.modal').on('shown.bs.modal', function () { $('.modal-open').attr('style','overflow:visible !important;'); document.body.scrollTop = default_scrollTop; })

2297028416 avatar May 29 '17 02:05 2297028416

.modal-open{ position: relative; }

Worked for me =)

AlonsoK28 avatar Apr 03 '18 17:04 AlonsoK28

Remove position fixed.

body .modal-open{ position:fixed; position:relative; }

binu-webaxyz avatar Jun 08 '18 06:06 binu-webaxyz

Replaced # with javascript:; in this statement and it worked. <a href="javascript:;"

alb9913 avatar Jun 21 '18 19:06 alb9913

Followed solution by: @kiranpatil353 and @cpuguy83 and @umbe1987 and worked

a="#.." makes the browser jump to the top of page. https://stackoverflow.com/questions/7755088/what-does-href-expression-a-href-javascript-a-do

To avoid this behavior you can also add e.preventDefault() on the click event of the anchor tag. This worked pretty well!! https://stackoverflow.com/questions/3252730/how-to-prevent-a-click-on-a-link-from-jumping-to-top-of-page

SabaKathawala avatar Sep 24 '18 18:09 SabaKathawala

I had the same problem and I realized, it was because of html{ overflow: scroll; } in my css. I hope you find this useful.

I changed this to body{ overflow-y: scroll; } - did the trick.

capesean avatar Nov 29 '18 11:11 capesean

I think this is because your href is set to "#". Changing this fixed it for me.

You could also change the single '#' to using multiple hashes '###'. Fixed it for me.

yohanelly95 avatar May 15 '19 06:05 yohanelly95

html{overflow: inherit} body.modal-open{overflow:hidden}(just set to body,not html and body);

sennpang avatar Jul 08 '19 10:07 sennpang

for WP please use

body.mobile.modal-open #wpwrap { overflow: hidden; position: relative; height: auto; }

Andrei0401 avatar Jan 30 '20 10:01 Andrei0401

I think this is because your href is set to "#". Changing this fixed it for me.

Thanks a lot!!!

ice19942335 avatar Feb 02 '20 19:02 ice19942335

use this code in Head

GanpatThakor1529 avatar Jan 08 '21 05:01 GanpatThakor1529

Removing the "href="#myModal" and replacing with "data-target="#myModal" fixed this for me.

bkaminski avatar May 06 '21 15:05 bkaminski

use this code in Head

It worked. Thanks @GanpatThakor1529

yrdesai avatar Jul 23 '21 12:07 yrdesai

It worked. Thanks.

yrdesai avatar Jul 23 '21 12:07 yrdesai

The solution to this years old problem is quite simple (I spent hours searching for it too):

make sure that the body tag only has 'position: relative;' set via CSS and no height definition. Then you can also keep the default setting from bootstrap in the .modal-open class.

Because as soon as 'height: 100%;' is defined on the body tag, the undesired behavior occurs. If you want to make sure that the body always fills 100% of the window, you should set 'min-height: 100vh;' on the body tag. This definition has no influence on the document behavior when opening the modal.

freakkiller avatar Apr 28 '22 09:04 freakkiller

@rudrarakeshpaul Thanks Bud You Saved me <3

kmt9967 avatar Aug 11 '22 15:08 kmt9967