Set div position fixed without page jumping up - javascript

I have a modal dialog (jquery.dialog) that opens up when a user clicks on a link. I want to hide the scroll bar on the page behind the dialog, so user will not be able to use it. This works perfectly fine with the overflow:hidden except older IE browsers that mess the entire page up when doing that.
So I came up with this to make it work in the IE: I created a div that was covering the entire content of the page and set it to position:relative and width/height:100%. Then anytime, the modal dialog opens up, the position gets set to fixed. That makes the overwflow:hidden work in old IE.
Now, the other problem came up. When the user is at the bottom of the page and clicks on the links, dialog pops up, but the main page jumps up to the top.
I want the main page to stay intact if possible. How can I do so?
Thank you.
The code to show/close the dialog:
show:
$('#allContent').css({ position: "fixed" });
$('#viewJobPanel').dialog('open');
$('#allContent').css('overflow', 'hidden');
close:
$('#allContent').css('overflow', 'visible');
$('#allContent').css({ position: "relative" });
$('#viewJobPanel').dialog('destroy');
and when user clicks on the link, I have this event handlers
e.preventDefault();
e.stopPropagation();

Use a span bound to a click event instead of an anchor element to avoid the default behavior of repositioning the top of the window.

Related

Modal position changes depending on trigger location

I am building a web widget that opens a modal window. The window opens as expected when the trigger link is at the top of the page. But when the trigger is at the bottom of a long page (that requires scrolling), the modal window is clipped/truncated above the viewing area.
Regardless of where the modal window is triggered, the HTML is appended to the body of the page, so I don't understand what's causing the issue.
I would appreciate some help.
js code: https://textfilter.me/widget.js
example / demo : https://textfilter.me/privacy/
It's not javascript. It's css. In this case change position:absolute to position: fixed in modal class. voila.

jquery modal prevent scrolling AND prevent moving back to top of page on modal open

There are two problems that are both independently well-documented, but the solutions appear to be mutually exclusive from what I can tell.
The first is that when we open a modal, we want to be able to stop the screen from scrolling, which is prevented by doing something akin to this: disable browser scrolling while jQuery UI modal dialog is open
There is a second problem, which is that when the modal opens, the screen is forced to scroll back to the top of the page, which can be prevented by using the following: Prevent CSS Modal from scrolling to top
i.e. to solve the first issue, adding the following to body css solves the issue:
overflow:hidden;
and to solve the second issue, adding the following to body css solves the issue:
overflow:visible;
The problem I face is that I want both to be true. When the modal opens, I want the scrolling to be disabled, AND I want to have the page freeze at the place the user had scrolled to, rather than jumping back to the top. Neither of these solutions will allow both of these actions simultaneously.
Does anybody know of a solution that would solve these two at the same time?
My solution:
To prevent scroll. When open the dialog add a class to body like:
.modal-open {
overflow: hidden;
}
And remove class when close the dialog.
To prevent moving top. In jquery, in the function where you open dialog, add preventDefault

IE -- anchor tags don't work after body changed to overflow hidden and then unset

I have a webpage that has some text and some anchor tags with external links. The page is long enough that you have to scroll (vertically) to see everything on it.
I have a modal on my page that, when clicked on, adds this the class scroll-override to the body:
.scroll-override {
overflow: hidden;
}
This prevents the page from scrolling when the modal is open. When the modal is dismissed, this class is removed from the body and I can scroll again.
On IE 11/10, when the modal is dismissed, I scroll down the page to the part of the page that was not visible when the modal was open. I click on a link, and nothing happens. No error in the console, no nothing.
This only happens in IE. On the other browsers, I am able to click the links after showing and dismissing the modal, scrolling down, and clicking them.
Any ideas on why this might be?

Twitter Bootstrap: Prevent Body from scrolling when a modal is opened

The issue I'm having is that when a modal is opened, the background body is scrollable using the mouse wheel.
Seems like this problem is known and people have suggested to set the body to overflow:hidden as stated in this link:
Prevent BODY from scrolling when a modal is opened
which works fine if your page is short and the modal link is on the initial visible page. However, if you have a longer page and you have to scroll down to see the modal link, once you click to open the modal, the background body shifts to the top.
The background does not scroll anymore, which is what I want, but is there any way to prevent it from popping back to the top when the modal is opened? It's inconvenient when you need to add multiple entries of something using the modal and you have to keep scrolling down to click the modal link to add another item.
In your onclick(I'm guessing you use onclick) event-method insert a return false; at the end, that will prevent the site from scrolling to the top.
I was having a similar problem in which modals larger than the window were cut off, and scrolling anywhere would scroll the background and not the modal.
This question pointed me to this plugin which is simple to use and fully addresses mine in addition to your problem of not permitting the background to scroll:
However this issue is said to be resolved in Bootstrap 3 and the plugin should not be needed if you're using the current version of Bootstrap.

Stop page from scroll when changing page contents using Jquery

I am toggling a div using jquery. when this div appears on page my browser scroll bar goes to top, even i am on last of my page. can you help me to stop this scroll where it is even more and more divs appears on page using jquery
i shall be thank full
I assume the link which is being clicked that controls the div toggle is set as href="#". If this is the case, add preventDefault() to the jQuery click event handler to stop the link behaviour from making the page scroll to the top.
$("#myLink").click(function(e) {
e.preventDefault();
// toggle your div
});
you need to place all the divs that scroll inside another div that has a fixed height.

Categories