I have 3 divs that toggle when their relative link is clicked. When a div is toggled and another is already active, it replaces the "previous":
<script>
jQuery(document).ready(function(){
jQuery(".toggleddivs").hide();
jQuery('.togglelink').click(function(){
jQuery(".toggleddivs").hide();
jQuery("#"+jQuery(this).data('target')).slideToggle();
});
});
</script>
<a class="togglelink" data-target="div1">togglelink1</a>
<a class="togglelink" data-target="div2">togglelink2</a>
<a class="togglelink" data-target="div3">togglelink3</a>
<div class="toggleddivs" id="div1">CONTENT_DIV1 <a class="toggle" data-target="div1">CLOSE</a></div>
<div class="toggleddivs" id="div2">CONTENT_DIV2 <a class="toggle" data-target="div2">CLOSE</a></div>
<div class="toggleddivs" id="div3">CONTENT_DIV3 <a class="toggle" data-target="div3">CLOSE</a></div>
DEMO (it's like this in my live site): http://jsfiddle.net/RKVY7/
It works fine, except that
1) I don't know how to make the close button to work (see demo)
2) when the div is toggled the page moves up. How can I "block" the position of the page?
1) You will need to change the class name w/in your divs from "toggle" to something else like "close" for instance, and then add this: (see example )
jQuery('.close').click(function(){
jQuery(".comefareper").slideUp( "slow", function() {
$(this).hide();
});
});
2) You will need to save the current scroll before your code run and once it's done set it back to the saved position:
jQuery(document).ready(function(){
jQuery(".comefareper").hide();
var currentScrollTop = $(window).scrollTop();
jQuery('.toggle').click(function(){
jQuery(".comefareper").hide();
jQuery("#"+jQuery(this).data('target')).slideToggle();
$(window).scrollTop(currentScrollTop);
});
jQuery('.close').click(function(){
jQuery(".comefareper").slideUp( "slow", function() {
$(this).hide();
$(window).scrollTop(currentScrollTop);
});
});
});
So after all i manipulated plenty of your JS-code to beware you from the callback-hell . After all you should use classes for the elements. It was hard to look through your code. But heres the result:
jsfiddle (http://jsfiddle.net/RKVY7/7/)
HTML
<a class="togglelink" data-target="div1">togglelink1</a>
<a class="togglelink" data-target="div2">togglelink2</a>
<a class="togglelink" data-target="div3">togglelink3</a>
<div class="container">
<div class="toggleddivs" id="div1">CONTENT_DIV1 <a class="toggle" data-target="div1">CLOSE</a></div>
<div class="toggleddivs" id="div2">CONTENT_DIV2 <a class="toggle" data-target="div2">CLOSE</a></div>
<div class="toggleddivs" id="div3">CONTENT_DIV3 <a class="toggle" data-target="div3">CLOSE</a></div>
</div>
CSS (for .container)
.container {
display: inline-block;
padding: 0;
margin : 0;
}
JS
jQuery(document).ready(function(){
// INT
// the height of the highest one
var highestcomefareper = 0;
// get highest comefareper so it will not scroll up.
var _gethighestcomefareper = function () {
var _height = jQuery(this).height();
if(highestcomefareper<_height) {
highestcomefareper = _height;
}
};
// It's not a "real toggle". Just close all and slide down the one
var _togglecomefareper = function(){
var _target = jQuery(this).data('target');
jQuery(".comefareper").hide();
jQuery("#"+_target).slideDown();
};
// close the nearest element with the class "comefareper". So don't use it in these boxes again
var _closecomefareper = function () {
jQuery(this).closest('.comefareper').slideUp();
};
// Get highest comefareper
jQuery(".comefareper").each(_gethighestcomefareper);
// Assign height to the container. (Prevents page from up-scrolling on close)
jQuery(".container").css("min-height", highestcomefareper + "px");
// Hide All
jQuery(".comefareper").hide();
// Assign functions to the buttons
jQuery('.toggle').click(_togglecomefareper);
jQuery(".close").click(_closecomefareper);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.