Prevent jQuery from Scrolling on Fixed Nav [duplicate] - javascript

This question already has answers here:
Prevent page scroll after click?
(3 answers)
Closed 8 years ago.
I'm working with a site that has a fixed navigation bar at the top. The search input field uses jQuery to slide out in nav bar when the icon is clicked. When the user scrolls down on the page and clicks the icon, the page automatically scrolls back to the top. Is there a simple way to prevent this behavior in the event handler below?
$('.search-button').on('click', function(){
$(this).hide();
$('.search-field').animate({'width':'toggle'});
});

$('.search-button').on('click', function(e){
e.preventDefault(); //If this method is called, the default action of the event will not be triggered.
$(this).hide();
$('.search-field').animate({'width':'toggle'}); });
pass the event e as argument and cancel the default behaviour
that will work

Related

Do not close window after clicking 'X' button? [duplicate]

This question already has answers here:
Prompt User before browser close?
(4 answers)
Closed 9 months ago.
I want to show a dialog when users click the X button to close the browser's window. In the dialog, it will ask users if they want to proceed with closing or not, if users choose no, it won't close the window.
I am wondering how to achieve this. I tried the unload event handler, the code was triggered but the window has already been closed. I also tried onbeforeunload, but it seems not triggered at all.
window.addEventListener('onbeforeunload ', () => {
// code not triggered here
});
window.addEventListener('unload', () => {
// code triggered but window is already closed
});
Even if we assume that there is an event handler which will be triggered before the window is closed, then what code should I write in order to prevent closing the window? It looks like once X button is clicked, the window is just "determined" to close and it's not reversible?
[https://stackoverflow.com/questions/2923139/prompt-user-before-browser-close]
this is the basic way to do
but prompt will be always the same
window.onbeforeunload = function(evt) {
return true;
}

Stop navigation AFTER link has been clicked

I have a slider with some buttons (which are links).
The buttons have double function
If I click on an inactive button, the slider will slide to the appropriate slide and the button will turn active (so preventDefault() here)
If I click on an active button, the link will work (no preventDefault())
Now, when I click on another inactive link after an active link is clicked, the browser will still got to the active link's url.
So the question is: How can I abort/stop/cancel a link navigation, after the link was pressed? I.E. my browser is already working.
The question is not: How can I prevent the link to be followed, if I click on the link?
jQuery('a.button').on('click', function(e){
if ( jQuery(this).hasClass('active') ) {
// no preventDefault(), so link is followed
return;
}
e.preventDefault();
// how can I stop the link from above?
// ..
jQuery(this).addClass('active')
// do the slide thing
});
Sounds like what you're trying to do is window.stop().
https://developer.mozilla.org/en-US/docs/Web/API/Window/stop
The stop() method is exactly equivalent to clicking the stop button in
the browser. Because of the order in which scripts are loaded, the
stop() method cannot stop the document in which it is contained from
loading, but it will stop the loading of large images, new windows,
and other objects whose loading is deferred.

Allow scrolling only when side panel is open

I'm using jquery mobile's panel feature to create a slide out menu for my mobile app but the number of links in the panel excedes the page length. I also have an event listener in place to prevent scrolling, but it interferes with scrolling to the other links. So what I wanted was to enable the event listen only when the panel was closed and remove it when it was opened. So I came up with this.
$('#panel.ui-panel-closed').addEventListener('touchmove', function(e) {
e.preventDefault(); }, false);
So when ever the #panel has a class of ui-panel-closed, the event listen is in placed. But whats ended up happening is that I have to open and close the panel first before it is effected by the javascript. Any ideas on how to get it to work on load. It is wrapped around a on document ready statement.
Try it like this:
Add it into
$(document).on("pageinit", function() {
$(document).on("touchmove", "#panel.ui-panel-closed", function(e) {
e.preventDefault();
});
});

Stop click from working on inner elements [duplicate]

This question already has answers here:
How to have click event ONLY fire on parent DIV, not children?
(12 answers)
Closed 10 years ago.
I have a div that covers the screen. Within the div is another div and within that, an image get dynamically placed. When the user clicks on the outer div it closes, and that is fine. When the user clicks on the image or the inner div it closes as well, how can I stop that from happening? I only want it to close if they click on the outer div.
This is what I am using; what do I need to do to make this work?
$("#black-out").click(function(){
$(this).fadeOut("slow");
});
The HTML:
<div id="black-out"><div id="image-holder"></div></div>
Check if the clicked element is the same as the one the event was bound to :
$("#black-out").click(function(e){
if (e.target == this) $(this).fadeOut("slow");
});

Hiding divs when clicking outside an image [duplicate]

This question already has answers here:
Detect click outside element?
(2 answers)
Hide div when clicking outside
(3 answers)
Closed 10 years ago.
I have a webpage with several hidden divs. I have them set up so that when an image is clicked, they will display and if the image is clicked again, they hide. The problem is, I need to hide them if they're visible and any part of the page is clicked. I've searched high and low and have found some suggestions but have yet to find one that works. Can anyone help?
$(window).click(function() {
$('img').hide();
});
Very simple example
I usually bind a document.click event to listen for a click outside and then remove it when the window is closed.
// in function after your image shows up.
document.click = hideImage;
// other code to hide image when image is clicked
// in function after your image is hidden.
document.click = null;
If you're using jQuery it's easier because you can namespace your events for safe add and removal.
// in function after your image shows up.
$(document).bind('click.imagehide', hideImage);
// other code to hide image when image is clicked
// in function after your image is hidden.
$(document).unbind('click.imagehide');
This method is safer so you don't interfere with other click events bound to the document.
What you need to do is prevent event bubbling. If you are using jquery you can do:
$(document).on ('click', function () { $('div').hide (); });
$('img').on ('click', function (e) { e.stopPropagation (); });
Remeber to change the selectors to fit your needs.

Categories