I'm trying to learn how to use jQuery, and I've stumbled upon a problem. First off, I will show you the part of the code that causes the problem.
HTML
<div id="nav">
<div id="button"><p class="scroll" onclick="location.href='#ed';">Education</p></div>
<div id="button"><p class="scroll" onclick="location.href='#wxp';">Work Experience</p></div>
<div id="button"><p class="scroll" onclick="location.href='#oact';">Other Activities</p></div>
<div id="button"><p class="scroll" onclick="window.open('cv.pdf','mywindow');">View as PDF</p></div>
<div id="arrow_container"><div class="arrow" id="down"></div></div>
jQuery
$(document).ready(function(){
$("#arrow_container").toggle(
function () {
$("#nav").animate({marginTop:"0px"}, 200);
}, function () {
$("#nav").animate({marginTop:"-100px"}, 200);
});
});
I want the div #nav
, which is initially positioned partially outside of the screen, to move down when div #arrow_container
is clicked. Then when #arrow_container
is clicked again I want #nav
to move up, to its original position.
At the moment, non of this happens. Can you tell me what the problem is and how I can fix it?
EDIT: a jsfiddle with the code, including some css
EDIT 2: The problem seems to be solved. Also thanks to someone whose username I forgot and answer has been deleted, but he had some great tips! Thank you!
toggle() changes from display:none to display:block , a property which is not animatable. Use toggleClass() to ...well...toggle :)/change classes on the menu and style those classes. So use left:-100% and left:200px ( if that is where you want to position the menu ) and together with transition or maybe opacity.
The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden.
jQuery slideToggle() Method The slideToggle() method toggles between slideUp() and slideDown() for the selected elements. This method checks the selected elements for visibility. slideDown() is run if an element is hidden. slideUp() is run if an element is visible - This creates a toggle effect.
click(function() { $("input[name=recipients\\[\\]]"). attr('checked', true); }); });
Try this:
$("#arrow_container").click( function(event){
event.preventDefault();
if ( $(this).hasClass("isDown") ) {
$("#nav").stop().animate({marginTop:"-100px"}, 200);
} else {
$("#nav").stop().animate({marginTop:"0px"}, 200);
}
$(this).toggleClass("isDown");
return false;
});
http://jsfiddle.net/us6sohyg/5/
for me that didn't work to 100% i had to edit a stop() event before every animate. so:
$("#arrow_container").click( function(event){
event.preventDefault();
if ($(this).hasClass("isDown") ) {
$("#nav").stop().animate({marginTop:"0px"}, 200);
$(this).removeClass("isDown");
} else {
$("#nav").stop().animate({marginTop:"-100px"}, 200);
$(this).addClass("isDown");
}
return false;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With