jQuery slideToggle confusion - javascript

My problem:
Each individual header section opens a content section. One button expands and collapses all those sections. However, the button is not functioned the way I would like it. Instead, it opens all the closed sections and if any section was left open - it closes it.
Code:
$(".panelStyle").hide();
$("#ExpandAndCollapseButton").click(function () {
$(".panelStyle").slideToggle("medium");
});
$(".sectionLabels").click(function () {
$(this).next(".panelStyle").slideToggle("medium");
});

You don't want to use slidetoggle as a "force all open" animation. You want to use slideDown()
slideToggle will just toggle the current state. slideDown will force any that aren't open to open, but leave the ones that are open alone.
Please see my change below.
$(".panelStyle").hide();
$("#ExpandButton").click(function () {
$(".panelStyle").slideDown("medium");
});
$(".sectionLabels").click(function () {
$(this).next(".panelStyle").slideToggle("medium");
});
I added some logic to toggle all open/close. If none are open, then all are opened. If one or more is already open, the all are closed.
Here is the fiddle
http://jsfiddle.net/QwbNj/1/
Here is the code.
$("#clickme").click(function () {
if($(".openme:visible").length == 0){
$(".openme").slideDown();
} else {
$(".openme").slideUp();
}
});
$(".menuwrapper").click(function () {
$(this).find(".openme").slideToggle();
});

Related

How can I close my accordion in a normal slideUp behavior ?

I have an accordion.
If I click on View Report - it will expand like this
If I click Hide Details - it will be back to original state.
JS
$saReport.click(function() {
console.log('R');
$saReport.addClass('hidden'); //hide
$saHide.removeClass('hidden'); //show
});
$saHide.click(function() {
console.log('H');
$saHide.addClass('hidden');
$saReport.removeClass('hidden');
$('.panel-collapse').removeClass('in');
});
Result
My accordion is closing very quick, like flashing hide.
How can I close my accordion in a normal slideUp behavior ?
Any hints / helps / suggestions on that will be much appreciated.
It would be usefull if you could post some relevant html.
If you you desire a simple slide behaviour you could either use the bootstrap accordion which is quite well documented.
If you want to implement it yourself, you could simply use the jquery slideUp() and slideDown() methods, i.e.:
$saReport.click(function() {
console.log('R');
$saReport.addClass('hidden'); //hide
$saHide.removeClass('hidden'); //show
// slide down your report (show)
$('.your-report-container').slideDown(400);
});
$saHide.click(function() {
console.log('H');
$saHide.addClass('hidden');
$saReport.removeClass('hidden');
$('.panel-collapse').removeClass('in');
// slide up your report (hide)
$('.your-report-container').slideUp(400);
});
These slide methods allow you to pass a parameter, which is the time in ms for the animation. The default is 400.

slide should take place from top to bottom

I am working on a hide-and-show functionality. I have two bugs in my code:
When I click the second div, the content is opening from down to top. How to make it from top to bottom?
When I click first it opens but when I click back first it does not close. How to fix it?
Providing my code below.
http://jsfiddle.net/2syzQ/33/
$(document).ready(function() {
$("#firstRadio").click(function() {
$("#secondHiddenDiv").hide("slow");
$("#firstHiddenDiv").show("slow");
});
$("#secondRadio").click(function() {
$("#firstHiddenDiv").hide("slow");
$("#secondHiddenDiv").show("slow");
});
$("#thirdRadio").click(function() {
$("#firstHiddenDiv, #secondHiddenDiv").hide("slow");
});
});

When I click to open "sidr side menu" I´m also clicking on my sidr menu item

I followed the sidr documentation at: http://www.berriart.com/sidr/
And I already have my sidr side left menu working fine.
But on my mobile,only on android default browser, when I click in my link "Open Menu" I also click on my menu item "Menu 1", and so it opens my submenu items with my toggle effect. And I dont want this.
I just want to open my submenu items when I click in my Menu items, and not in my link to open the menu.
I found a solution, that is, if I put my sidr menu with some margin top, to not align with my link to open the menu, the problem is solved, like in my second image.
But I dont want to give that margin-top, so Im trying look for other solution.
Somebody there have exprience with this plugin and can give me a help??
(This only happens in mobile and on android browser that cames when you buy the smartphone, but I want to use this on mobile, and many users must use internet explorer which I think is the default browser for android.)
Like this image below, I have the problem, because the "Open Menu is aligned with "Menu 1" and so Im clicking on both!
Like this image below, I dont have the problem, because the "Open Menu is not aligned with "Menu 1" and so I only click on "Open Menu"!
This is my jQuery to start sidr plugin:
$(document).ready(function() {
$('#simple-menu').sidr({
name: 'sidr',
speed: 200,
side: 'left',
source: null,
renaming: true,
body: 'body'
});
});
$(document).ready(function() {
$('.sub-menu-sidr').hide();
$("#sidr li:has(ul)").click(function(){
$("ul",this).toggle('fast');
});
});
And here is my fiddle:
http://jsfiddle.net/y4CX4/1/
Easiest way to do that, IMHO is to prevent the first click on that link from happening, that is:
Define a variable to check if link was clicked, at click event check the value and prevent the event from propagating and then set the variable to something else, in order to allow all future clicks to happen naturally, for example:
var click = false;
$('#sidr > ul > li').first().find('a').first().click( function(e) { if ( click == false ) {
e.stopPropagation();
click = true;
} });
The next step would be to add a function that resets this variable when the menu gets closed by adding:
onClose : function() {
click = false;
}
An working example can be found here: http://jsfiddle.net/y4CX4/3/
Also make sure you use the same function in order to use the variable click properly ( in the fiddle you posted you used $(document).ready() two times for some reason ).
My solution to that problem is based on the top answer which helped me find the right way.
So I find all the links and prevent their default behavior until the menu is opened and then disable them again when the menu is closed.
var menuButton = $('.js-side-menu-toggle');
var sideMenuLinks = $('#sidr').find('a');
var canClick = false;
sideMenuLinks.on('click', function(e) {
if (!canClick) {
e.preventDefault();
}
});
menuButton.sidr({
onOpen: function() {
canClick = true;
},
onClose: function() {
canClick = false;
}
});
The tricky part here is that we need to change the plugin itself so that this code can work.
The problem is that the functions onOpen() and onClose() are called after the animation is done but not in it's callback function. That makes the functions to be called with the animation which is async and here is our issue.
Wrong:
// Close menu
if($body.is('body')){
scrollTop = $html.scrollTop();
$html.removeAttr('style').scrollTop(scrollTop);
}
$body.addClass('sidr-animating').animate(bodyAnimation, speed).removeClass(bodyClass);
$menu.animate(menuAnimation, speed, function() {
$menu.removeAttr('style').hide();
$body.removeAttr('style');
$('html').removeAttr('style');
sidrMoving = false;
sidrOpened = false;
// Callback
if(typeof callback === 'function') {
callback(name);
}
$body.removeClass('sidr-animating');
});
// onClose callback
onClose();
We just need to insert the onClose function inside the animation callback function in order to lock the links when the menu is closed and we should do the same with the on open code fragment.
Right:
// Close menu
if($body.is('body')){
scrollTop = $html.scrollTop();
$html.removeAttr('style').scrollTop(scrollTop);
}
$body.addClass('sidr-animating').animate(bodyAnimation, speed).removeClass(bodyClass);
$menu.animate(menuAnimation, speed, function() {
$menu.removeAttr('style').hide();
$body.removeAttr('style');
$('html').removeAttr('style');
sidrMoving = false;
sidrOpened = false;
// Callback
if(typeof callback === 'function') {
callback(name);
}
$body.removeClass('sidr-animating');
// onClose callback
onClose();
});

Collapsible Menu Not Staying Open

Hello ive followed the instructions from this webspage Add a Blogger-like collapsible archive block to your Drupal 7 site and suprised myself that everything seems to be 'sort of' working. As you can see from my 'collapsible block' on the right of THIS PAGE that the block doesnt seem to want to stay open when viewing other months. I dont think this was the intended behaviour.
jQuery(document).ready(function($) {
// init: collapse all groups except for the first one
$(".view-collapsible-archive ul").each(function(i) {
if (i==0) {
$(this).siblings("h3").children(".collapse-icon").text("▼");
} else {
$(this).hide();
}
});
// click event: toggle visibility of group clicked (and update icon)
$(".view-collapsible-archive h3").click(function() {
var icon = $(this).children(".collapse-icon");
$(this).siblings("ul").slideToggle(function() {
(icon.text()=="▼") ? icon.text("►") : icon.text("▼");
});
});
});
Could anyone suggest anything to me to make the menu block open on a month when clicked and to close the other 'months'?
thanks
The problem is that the code you have is already added inside the file http://netmagpie.com/sites/all/themes/feverultra/js/feverultra.js and by adding your file after that, you're binding twice to the event and the function toggles twice, so the elements open and close
If you want to only have one month open then you need to close any open months before opening the one that was clicked, something like:
jQuery(document).ready(function($) {
// init: collapse all groups except for the first one
$(".view-collapsible-archive ul").each(function(i) {
if (i==0) {
$(this).siblings("h3").find(".collapse-icon").text("▼");
} else {
$(this).hide();
}
});
// click event: toggle visibility of group clicked (and update icon)
$(".view-collapsible-archive h3").click(function() {
$('.view-collapsible-archive ul:visible').not($(this).next('ul')).slideUp();
var icon = $(this).find(".collapse-icon");
$(this).siblings("ul").slideToggle(function() {
(icon.text()=="▼") ? icon.text("►") : icon.text("▼");
});
});
});
It's because of this line:
$(this).siblings("ul").slideToggle
It says: "toggle the state of all the ul elements using a slide animation"
You will want to change this to slideDown when it's hidden in order to show it and slideUp when it's visible in order to hide it.
I would provide a code sample but I'm typing with one thumb on an iPhone at the moment.

jQuery toggle content-div from menubar

I'm having trouble getting jQuery to allow only one content-DIV to be visible at a time. So that clicking between the menu buttons (about, newsletter, contact) this will allow only one content-DIV to be visible.
--- Then the content-DIV needs to hide when the associated menu button is clicked (like it does currently).
--- Upon clicking the header 'alliteration', any content-DIVs that are open need to hide.
$('#collapse_about').hide();
$('#collapse_newsletter').hide();
$('#collapse_contact').hide();
$('#menu1').click(function() {
$('#collapse_about').slideToggle(400);
return false;
});
$('#menu2').click(function() {
$('#collapse_newsletter').slideToggle(400);
return false;
});
$('#menu3').click(function() {
$('#collapse_contact').slideToggle(400);
return false;
});
I understand that this is a pretty simple bit of code... but the form of it evades me. Any help is much appreciated.
Off the top of my head I would assign all of the menus the same class such as class1. Then make the div slideUp which has a callback function. When the slideUp is finished it will slide down the correct panel.
$('#menu1').click(function() {
$(".class1").slideUp(function() {
$('#collapse_about').slideDown(400);
});
});
$('#menu2').click(function() {
$(".class1").slideUp(function() {
$('#collapse_newsletter').slideDown(400);
});
});
$('#menu3').click(function() {
$(".class1").slideUp(function() {
$('#collapse_contact').slideDown(400);
});
});
Edit: This is a start but doesn't entirely do as you are asking:(
Here is the demo on jsfiddle: here

Categories