I would like to create a slide down button to show the content of a DIV that has no height. I did an example here:
fiddle
I looked for this special form of sliding buttons but all I have found were examples where a DIV and its content has given width and height.
In my example the structure is different to that but it is quiet simple. There are three different divisions. A head with given height, the content div (depends from padding) that should be shown and below that the slider/trigger (also with given height) as a button.
Now I would like click on the trigger that slides down about the height of the content div and finally the content div should be appear.
It would be great if someone could help me out.
Thanks alot.
I think this is what you are looking at.
Fiddle
$(document).ready(function () {
$('.content').hide(); //hide initialy
$('.slide').click(function () {
var $this = $(this);
// target only the content which is a sibling
$(this).siblings('.content').slideToggle(200, function () {
$this.text($(this).is(':visible') ? 'click to close' : 'click to open');
});
});
});
You should try slideToggle() instead (it also helps when jQuery is loaded, but that was probably just an oversight when you made it):
$(document).ready(function () {
$('.slide').click(function () {
$(this).prev('.content').slideToggle(500, function () {
$this = $(this);
$slide = $('.slide');
if ($this.is(':visible')) {
$slide.text('Click to close');
} else {
$slide.text('Click to open');
}
});
});
});
fiddle
Related
I have the following code in my JS file:
jQuery("document").ready(function (e) {
var menu = e(".menu-container");
var button = e(".menu-functions");
e(window).scroll(function () {
if (e(this)
.scrollTop() > 150) {
menu.addClass("f-nav");
button.addClass("collapse-expand");
button.addClass('collapse');
} else {
menu.removeClass("f-nav");
button.removeClass("collapse");
button.removeClass("expand");
button.removeClass("collapse-expand");
}
});
//problem area
$('#menu-functions').click(function(){
if(button.hasClass('collapse'))
{
button.addClass('expand');
button.removeClass('collapse');
}
if(button.hasClass('expand'))
{
button.addClass('collapse');
button.removeClass('expand');
}
});
});
Now I need to make it so that the part under the // problem area starts to work. I reckon there's a toggleClass in jQuery, right? Some advanced conditions could do the trick, however I'm still learning and I need some help. I also need to find a way to animate() the .menu-container div whether the button state is expand or collapse:
If the button was clicked while it had the expand class
animate the menu from bottom to top with 98px;
If the button was clicked while it had the collapse class
animate the menu from top to bottom with 98px.
EDIT - JSFIDDLE:
jsfiddle.net/rcdhnh7L
Try it like this instead. Don't use e as the var for jQuery, that's just strange. And I simplified the problem area to directly grab the elements you want instead of iterating an existing collection.
jQuery("document").ready(function ($) {
var menu = $(".menu-container");
var button = $(".menu-functions");
$(window).scroll(function () {
if ($(this)
.scrollTop() > 150) {
menu.addClass("f-nav");
button.addClass("collapse-expand");
button.addClass('collapse');
} else {
menu.removeClass("f-nav");
button.removeClass("collapse");
button.removeClass("expand");
button.removeClass("collapse-expand");
}
});
//problem area
$('#menu-functions').click(function () {
$('.menu-functions.collapse').addClass('expand').removeClass('collapse');
$('.menu-functions.expand').addClass('collapse').removeClass('expand');
});
});
Or this should work as well:
//problem area
$('#menu-functions').click(function () {
$('.menu-functions.collapse, .menu-functions.expand').toggleClass('expand collapse');
});
I have a website where there is implemented a CSS overlay when the menu is accessed. This overlay fades in and out over the rest of the page but not the menu itself via the z-index. All works well on mouseover, the overlay fades in, and the rest looks dimmed.
However when the mouse is not on the menu anymore, the fadeout of the overlay is messed up. The overlay itself fades out correctly, but all images show up immediately (opacity 1?) and that makes it look strange. I think the images have to be fade-in when the overlay fades-out. You can see an example on www.appartement-tekoop.nl of the behavior.
I think the problem is with the z-index of the images, but am not sure.
This is my javascript for the overlay:
<script type="text/javascript">
jQuery(function () {
var $menu_main_nav = jQuery('#menu-main-nav');
var $menu_main_nav_items = $menu_main_nav.children('li');
var $oe_overlay = jQuery('#oe_overlay');
var $pricetable_dropdown = jQuery(".price-header");
var $menutoggle = jQuery('.menutoggle');
var $mainmenu = jQuery('.main-nav');
$menu_main_nav_items.bind('mouseenter', function () {
var $this = jQuery(this);
$this.children.addClass('slided selected');
// updated code starts
if($this.children('.menu-item-has-children').hasClass('not-shown')){
$menu_main_nav_items.not('.slided').children('.menu-item-has-children').hide();
$this.removeClass('slided');
}
else {
$this.children('.menu-item-has-children').css('z-index', '9999').stop(true, true).slideDown(200, function () {
$menu_main_nav_items.not('.slided').children('.menu-item-has-children').hide();
$this.removeClass('slided');
});
}
// updated code ends
})
.bind('mouseleave', function () {
var $this = jQuery(this);
$this.removeClass('selected').children('.menu-item-has-children').css('z-index', '1');
});
$menu_main_nav.bind('mouseenter', function () {
var $this = jQuery(this);
$oe_overlay.stop(true, true).fadeTo(1000, 0.3);
$oe_overlay.css('z-index', '40');
$this.addClass('hovered');
})
.bind('mouseleave', function () {
var $this = jQuery(this);
$this.removeClass('hovered');
$oe_overlay.stop(true, true).fadeTo(1000, 0);
$oe_overlay.css('z-index', '0');
$menu_main_nav_items.children('.menu-item-has-children').hide();
});
$pricetable_dropdown.bind('click', function() {
if (jQuery( this ).hasClass('clicked')) {
jQuery( this ).removeClass('clicked');
jQuery( 'section.detail-page' ).css('display', 'none');
jQuery( 'section.detail-page' ).css('display', 'block');
} else {
jQuery( this ).addClass('clicked');
// $initialDivHeight = jQuery('section.detail-page').height();
}
jQuery( this ).next().fadeToggle();
});
$menutoggle.bind('click', function() {
$mainmenu.toggle();
});
});
</script>
You're right, the z-index is messing things up... The Overlay is jumping behind some elements on the page before the transition has finished.
There is no need to change the overlays z-index on mouseover, remove this from your JS. Set the overlays 'z-index: 40' in your css and set it to 'display: none' also.
Thats all you need. When you fadeOut() an element it sets 'display: none' on it after it's finished animating, so your mouse won't be able to interact with it, and you can click on anything underneath it.
Hope this helps!
I've got an accordion with arrow icons indicating when a section is open and closed. Once the accordion is opened the arrow points down showing the content below it.
However I've created a close button that gets appended into each section. This sits at the bottom of every section in the accordion.
I want it so that once the close button is pressed the arrow changes it's state back to closed.
$(function() {
$('#accordion h3').each(function(){
var $set = $(this).nextUntil("h3");
$set.wrapAll('<div />');
});
$('#accordion').accordion({ collapsible:true, active:true, heightStyle:"content", disabled:true, animated: false});
$('.ui-accordion-header').bind('click',function(){
theOffset = $(this).offset();
$(window).scrollTop(theOffset.top - 50);
});
$('#accordion h3.ui-accordion-header').click(function(){
var _this = $(this);
$('.ui-accordion-header-icon', _this).toggleClass('ui-icon-triangle-1-e ui-icon-triangle-1-s');
_this.next().slideToggle();
return false;
});
$('.ui-accordion-content').append('Close<div class="clearfix"></div>');
$('.close').click(function(){
$(this).closest('.ui-accordion-content').toggleClass('ui-icon-triangle-1-s');
$(this).parent().slideUp('slow', function(){
$(window).scrollTop(theOffset.top - 50);
var hidecollapsebutton = true;
$('.ui-accordion-content').each(function(){
if($(this).css('display') == 'block')
{
hidecollapsebutton = false;
}
});
if(hidecollapsebutton)
{
$('.accordion-expand-all').show();
$('.accordion-collapse-all').hide();
}
});
return false;
})
});
Any help would be much appreciated. I can provide more information if it's needed. Thanks.
http://jsfiddle.net/EZT6A/
$('.close').click(function(){
$(this).closest('.ui-accordion-content').toggleClass('ui-icon-triangle-1-s');
As you could have found out yourself with a little simple debugging, $(this).closest('.ui-accordion-content') does not match any element here. (That’s because your close button is within div.ui-accordion-content, and the h3.ui-accordion-header is the previous sibling of that div element.)
Simple to fix: Go up to parent div (.ui-accordion-content), get previous h3 (.ui-accordion-header), and then the span (.ui-accordion-header-icon) element within it:
$(this).parents('.ui-accordion-content')
.prev('.ui-accordion-header')
.find('.ui-accordion-header-icon')
.removeClass('ui-icon-triangle-1-s')
.addClass('ui-icon-triangle-1-e');
http://jsfiddle.net/EZT6A/2/
I'd try changing click to on:
$(document).on("click", ".close", function(){
//change arrow state
});
It's because not every .close element exist as you binding click event.
I've tried to create a jQuery effect using fancy box to contain my content and within that is a large image with thumbnails below. What I was trying to make happen was when the thumbnails are clicked then the large image updates (see RACE Twelve image as an example). This works fine but the problem is when I go to another fancy box on my website (SEE RACE ONE box) then that image has been updated to be whatever thumbnail was clicked last.
I thought this might be event bubbling but preventing default hasn't helped.
I'm very new to jQuery and know that this is something stupid that I'm doing.
Any advice would be greatly appreciated? Thank you :)
Live version of page: http://www.goodwood.co.uk/members-meeting/the-races.aspx
jsfiddle for jQuery: http://jsfiddle.net/greenhulk01/JXqzL/
(function ($) {
$(document).ready(function () {
$('.races-thumbnail').live("click", function (e) {
$('.races-main-image').hide();
$('.races-image-wrap').css('background-image', "url('http://www.goodwood.co.uk/siteelements/images/structural/loaders/ajax-loader.gif')");
var i = $('<img />').attr('src', this.href).load(function () {
$('.races-main-image').attr('src', i.attr('src'));
$('.races-image-wrap').css('background-image', 'none');
$('.races-main-image').fadeIn();
});
return false;
e.preventDefault();
});
$(".races-image-wrap img").toggle(function () { //fired the first time
$(".races-pop-info").show();
$(this).animate({
width: "259px",
height: "349px"
});
}, function () { // fired the second time
$(".races-pop-info").hide();
$('.races-main-image').animate({
width: "720px",
height: "970px"
});
});
$('#fancybox-overlay, #fancybox-close').live("click", function () {
$(".races-pop-info").show();
$(".races-main-image").animate({
width: "259px",
height: "349px"
});
});
});
})(jQuery);
$('.races-main-image') will select all elements with that class, even the ones which aren't currently visible.
You can select the closest '.races-main-image' to the clicked element as per the code below (when placed inside the click event handler)
$('.races-main-image', $(e.target).closest('.races-fancy-box'))
So your new code should look like:
$('.races-thumbnail').live("click", function (e) {
var racesmainimage = $('.races-main-image', $(e.target).closest('.races-fancy-box'));
var racesimagewrap = $('.races-image-wrap', $(e.target).closest('.races-fancy-box'));
racesmainimage.hide();
racesimagewrap.css('background-image', "url('http://www.goodwood.co.uk/siteelements/images/structural/loaders/ajax-loader.gif')");
var i = $('<img />').attr('src', this.href).load(function () {
racesmainimage.attr('src', i.attr('src'));
racesimagewrap.css('background-image', 'none');
racesmainimage.fadeIn();
});
return false;
});
I've also removed your 'e.preventDefault();' return false; includes that, and was preventing e.preventDefault() from being executed in any case.
Code:
<script type="text/javascript">
$(document).ready(function () {
$('.box').on('click', function (e) {
e.preventDefault();
var $btn = $(this);
$btn.toggleClass('opened');
var heights = $btn.hasClass('opened') ? 300 : 100;
$(this).stop().animate({ height: heights }, 800);
//$(".sync_store_info_input_holder").toggle();
});
});
</script>
Using the above code i am able to accomplish what i need but i have a slight problem with the following line:
$(".sync_store_info_input_holder").toggle();
I need that to show only when the .box opens and not when it closes. I also need it so show only for the box that the user opens. I have 18 boxes that use the .box class and only ids are unique so the only box the user opens thats when the toggle function should work.
It seems like you can use the showOrHide version of toggle(). If not, make a jsFiddle of your code to explain the problem.
<script type="text/javascript">
$(document).ready(function () {
$('.box').on('click', function(e) {
e.preventDefault();
var $btn = $(this);
$btn.toggleClass('opened');
// Make whether the box is open a variable for reuse
var isOpen = $btn.hasClass('opened');
var heights = isOpen ? 300 : 100;
// Reuse $btn instead of $(this)
$btn.stop().animate({ height: heights }, 800);
// Important change
$(".sync_store_info_input_holder").toggle(isOpen);
});
});
</script>
Edit
I also need it so show only for the box that the user opens. I have 18 boxes that use the .box class and only ids are unique so the only box the user opens thats when the toggle function should work.
Do you mean that there is an .sync_store_info_input_holder element within each box? If so, you can change the line to
$(".sync_store_info_input_holder", this).toggle(isOpen);
to select only the element within the .box context. If it's outside of the .box, you'd have to show the HTML for us to give an example.