So, i have some animation actions, this is for my login panel box:
$('.top_menu_login_button').live('click', function(){
$('#login_box').animate({"margin-top": "+=320px"}, "slow");
});
$('.login_pin').live('click', function(){
$('#login_box').animate({"margin-top": "-=320px"}, "slow");
});
now i need to add some hiding action after click on body so i do this:
var mouse_is_inside = false;
$('#login_box').hover(function () {
mouse_is_inside = true;
}, function () {
mouse_is_inside = false;
});
for stop hiding this element on body click, and this for body click outside by login-box
$("body").mouseup(function () {
if (!mouse_is_inside) {
var login_box = $('#login_box');
if (login_box.css('margin-top','0')){
login_box.stop().animate({"margin-top": "-=320px"}, "slow");
}
}
});
Everything is fine but this panel animates after each body click, how to stop this and execute only one time? Depend on this panel is visible or not?
You'd normally do this sort of thing by checking if the click occured inside the element or not, not by using mousemove events to set globals :
$(document).on('click', function(e) {
if ( !$(e.target).closest('#login_box').length ) { //not inside
var login_box = $('#login_box');
if ( parseInt(login_box.css('margin-top'),10) === 0){
login_box.stop(true, true).animate({"margin-top": "-=320px"}, "slow");
}
}
});
And live() is deprecated, you should be using on().
Related
I have a issue where the .click from the div where my cancel button is nested in triggers when I click the button. This causes both the slideUp and slideDown to trigger at the same time, which results in the div staying visible. I've tried adding a state to prevent the div from sliding down again, but this does not have the desired effect.
$(add).click(function () {
var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
if(state == 0){
$(inputDiv).slideDown(300);
}
state = 1;
});
$(cancel).click(function () {
var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
$(inputDiv).slideUp(300);
state = 0;
});
https://jsfiddle.net/kkdoneaj/2/
Does anyone know how to work around this issue?
Use Event.stopPropagation() to stop the click from bubbling from the #cancel button to the parent #add div:
$("#cancel").click(function(e) {
e.stopPropagation();
...
});
https://jsfiddle.net/kkdoneaj/3/
you should put stop
Stop the currently-running animation on the matched elements.
$(add).click(function () {
var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
$(inputDiv).stop().slideDown(300);
});
$("#cancel").click(function (e) {
var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
$(inputDiv).stop().slideUp(300);
e.stopPropagation();
});
Others have commented with stopPropagation(), but you can also not expand #inputDiv unless it's already visible, like so:
$(function(){
$("#add").click(function () {
if($("#inputDiv").css('display') == 'none'){
$("#inputDiv").slideDown(1000);
}
});
$("#cancel").click(function () {
$("#inputDiv").slideUp(1000);
});
});
I have this JSfiddle and i need to slide in, when clicking on a div, and not when page is loaded. Simultaneously it should be possible to close by clicking anywhere outside the slide-in box.
$( document ).ready(function() {
$(function(){
$(".slide-in").addClass("active");
console.log($(".slide-in"));
});
});
I think the solution could be some kind of toggle system, but i can't figure out how to?
Thank you!
Opens the slider on click of .button. Closes it on click anywhere outside the slider (including the button)
var isOpened = false;
$(document).click(function(e) {
if(isOpened && e.target.className=='slide-in') {
$(".slide-in").removeClass("active");
isOpened = false;
} else if(!isOpened && e.target.className=='button'){
$(".slide-in").addClass("active");
isOpened = true;
}
});
Better is to use IDs. So your code would be:
<div id="slide-in"></div>
<div id="button"></div>
and the javascript:
var isOpened = false;
$(document).click(function(e) {
if(isOpened && e.target.id!='slide-in') {
$("#slide-in").removeClass("active");
isOpened = false;
} else if(!isOpened && e.target.id=='button'){
$("#slide-in").addClass("active");
isOpened = true;
}
});
You'll also need to change the CSS from classes to IDs
Try this.
var someDiv = document.getElementById('yourDiv');
someDiv.style.cursor = 'pointer';
someDiv.onclick = function() {
//do something
}
how to make div click-able?
https://jsfiddle.net/zer00ne/jne1rasb/
$(document).ready(function() {
$('.button').on('click dblclick', function(e) {
$('.slide-in').toggleClass('active');
e.stopPropagation();
});
$(document).on('click', function() {
$(".slide-in").removeClass("active");
});
});
I think this should do the trick.
Edit:
$( document ).ready(function() {
$(".button").on("click",function(){
if($(".slide-in").hasClass("active")){
$(".slide-in").removeClass("active");
}else{
$(".slide-in").addClass("active");
}
});
});
I have an element which I want to expand on click and then collapse on click outside and thus came up with the following code. However when I run this it will start to expand and then immediately collapse since both functions are called sequentially. I don't understand why and how to solve this.
jQuery(document).ready(function() {
var element = jQuery("#search-main");
var defaultWidth = jQuery("#search-main").css('width');
var expandWidth = "200px";
var fnSearch = {
expand : function() {
jQuery(element).animate({
width : expandWidth
});
jQuery(document).bind('click', fnSearch.collapse);
},
collapse : function() {
jQuery(element).animate({
width : defaultWidth
});
event.stopPropagation();
jQuery(document).unbind("click", fnSearch.collapse);
}
}
jQuery("#search-main").bind("click", fnSearch.expand);
});
You are having the problem because the #search-main click event is propagating to the document; i.e. first the #search-main click event triggers, then the document click event triggers. Click events do this by default. To stop this event propagation, you want to use http://api.jquery.com/event.stoppropagation/ in your expand function:
jQuery(document).ready(function() {
var element = jQuery("#search-main");
var defaultWidth = jQuery("#search-main").css('width');
var expandWidth = "200px";
var fnSearch = {
expand : function(event) { // add event parameter to function
// add this call:
event.stopPropagation();
jQuery(element).animate({
width : expandWidth
});
jQuery(document).bind('click', fnSearch.collapse);
},
collapse : function() {
jQuery(element).animate({
width : defaultWidth
});
jQuery(document).unbind("click", fnSearch.collapse);
}
}
jQuery("#search-main").bind("click", fnSearch.expand);
});
That said, Jason P's solution is better for what you want. It's more reliable and less messy, since you don't have to bind stuff to the document, which can easily become hard to track and cause conflicts with other code if you use that strategy habitually.
You could unbind the click event from the #search-main element after clicking, or stop the propagation of the event, but I would recommend binding to the blur and focus events instead:
http://jsfiddle.net/6Mxt9/
(function ($) {
$(document).ready(function () {
var element = jQuery("#search-main");
var defaultWidth = jQuery("#search-main").css('width');
var expandWidth = "200px";
$('#search-main').on('focus', function () {
$(element).animate({
width: expandWidth
});
}).on('blur', function () {
$(element).animate({
width: defaultWidth
});
});
});
})(jQuery);
That way, it will work even if the user tabs in or out of the field.
I am trying to stop a propagation of a animate function for one div, right now the opacity animation effects everything (html) and that is fine, but i want it to "skipp" one div. My code look like this!
jQuery:
var HTML = document.getElementsByTagName('html')[0];
$('.day-number').each(function() {
this.addEventListener('click', function(e) {
$('html').animate({opacity:0.5}, function() {
$(this).find('.input_info_cal').fadeIn(500);
});
e.stopPropagation();
});
});
$('.input_info_cal').each(function() {
this.addEventListener('click', function(e) {
e.stopPropagation();
});
});
HTML.addEventListener('click', function() {
$('html').animate({opacity:1}, function() {
$(this).find('.input_info_cal').fadeOut(500);
});
});
The div called ".input_info_cal" is the one that should not get opacity on it, but I just cant get it to work..
All help is thanked for!
I've built a dropdown menu that uses a slideUp event if the menu itself or anywhere in the body is clicked:
$('html, .currentPage').click(function() {
$('.currentMenu').slideUp('fast', function() { myFunction(); });
});
I also have a function inside the slideUp event callback.
However, my problem is that the function is being called on html or .currentPage click whether the slideUp event has occurred or not.
How can I make it so the function(); ONLY happens if the slideUp event runs?
$('html, .currentPage').click(function() {
var $currentMenu = $('.currentMenu');
if($currentMenu.is(':visible')) {
$currentMenu.slideUp('fast', function() { myFunction(); });
}
});
even more
$('html, .currentPage').click(function() {
var $currentMenu = $('.currentMenu');
if($currentMenu.is(':visible')) {
$currentMenu.slideUp('fast', myFunction);
}
});
You can also optionally use $currentMenu.css('display') != 'none' instead of $currentMenu.is(':visible').
The options are suggested based on the actual behaviour of slideUp(), described in the official documentation at http://api.jquery.com/slideUp/.
$('.currentMenu').slideToggle('fast', function() { myFunction(); });
You could add class open to your menu and slideUp() if hasClass('open'):
if ( $menu.hasClass('open') ) {
$menu.slideUp('fast', function(){ });
...
}