I have the code working for the show and hide the div
. How would I add two different icons as a sprite image for when the show and hide are active?
For example: + icon for show me, then a - icon for hide me.
Here is the code, I have: http://jsfiddle.net/BLkpG/
$(document).ready(function(){
$(".slidingDiv").hide();
$(".show_hide").show();
$('.show_hide').click(function(){
$(".slidingDiv").slideToggle();
});
});
Need to change image to the above when toggled to a + or -.
Thanks
this works:
CSS:
.show_hide {
display:none;
}
.plus:after {
content:" +";
}
.minus:after {
content:" -";
}
jQuery:
$(document).ready(function(){
$(".slidingDiv").hide();
$(".show_hide").addClass("plus").show();
$('.show_hide').toggle(
function(){
$(".slidingDiv").slideDown();
$(this).addClass("minus");
$(this).removeClass("plus");
},
function(){
$(".slidingDiv").slideUp();
$(this).addClass("plus");
$(this).removeClass("minus");
}
);
});
HTML:
<a href="#" class="show_hide">Show/hide</a>
<div class="slidingDiv" style="display: block;">
Check out the updated jQuery plugin for doing this: <a href="http://papermashup.com/jquery-show-hide-plugin/" class="show_hide" target="_blank" style="display: inline;">jQuery Show / Hide Plugin</a>
</div>
in the CSS, instead of content:" +";
You can put an background-image
(with background-position:right center;
and background-repeat:no-repeat
and maybe making the .show_hide
displayed as block and give him a width, so that the bg-image is not overlayed by the link-text itself).
Here is a quick edit of Enve's answer. I do like roXor's solution, but background images are not necessary. And everbody seems to forgot a preventDefault
as well.
$(document).ready(function() {_x000D_
$(".slidingDiv").hide();_x000D_
_x000D_
$('.show_hide').click(function(e) {_x000D_
$(".slidingDiv").slideToggle("fast");_x000D_
var val = $(this).text() == "-" ? "+" : "-";_x000D_
$(this).hide().text(val).fadeIn("fast");_x000D_
e.preventDefault();_x000D_
});_x000D_
});
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<a href="#" class="show_hide">+</a>_x000D_
_x000D_
<div class="slidingDiv">_x000D_
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta._x000D_
Mauris massa. Vestibulum lacinia arcu eget nulla. </p>_x000D_
_x000D_
<p>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis._x000D_
Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. </p>_x000D_
_x000D_
</div>
_x000D_
HTML:
<div class="Settings" id="GTSettings">
<h3 class="SettingsTitle"><a class="toggle" ><img src="${appThemePath}/images/toggle-collapse-light.gif" alt="" /></a>General Theme Settings</h3>
<div class="options">
<table>
<tr>
<td>
<h4>Back-Ground Color</h4>
</td>
<td>
<input type="text" id="body-backGroundColor" class="themeselector" readonly="readonly">
</td>
</tr>
<tr>
<td>
<h4>Text Color</h4>
</td>
<td>
<input type="text" id="body-fontColor" class="themeselector" readonly="readonly">
</td>
</tr>
</table>
</div>
</div>
<div class="Settings" id="GTSettings">
<h3 class="SettingsTitle"><a class="toggle" ><img src="${appThemePath}/images/toggle-collapse-light.gif" alt="" /></a>Content Theme Settings</h3>
<div class="options">
<table>
<tr>
<td>
<h4>Back-Ground Color</h4>
</td>
<td>
<input type="text" id="body-backGroundColor" class="themeselector" readonly="readonly">
</td>
</tr>
<tr>
<td>
<h4>Text Color</h4>
</td>
<td>
<input type="text" id="body-fontColor" class="themeselector" readonly="readonly">
</td>
</tr>
</table>
</div>
</div>
JavaScript:
$(document).ready(function() {
$(".options").hide();
$(".SettingsTitle").click(function(e) {
var appThemePath = $("#appThemePath").text();
var closeMenuImg = appThemePath + '/images/toggle-collapse-light.gif';
var openMenuImg = appThemePath + '/images/toggle-collapse-dark.gif';
var elem = $(this).next('.options');
$('.options').not(elem).hide('fast');
$('.SettingsTitle').not($(this)).parent().children("h3").children("a.toggle").children("img").attr('src', closeMenuImg);
elem.toggle('fast');
var targetImg = $(this).parent().children("h3").children("a.toggle").children("img").attr('src') === closeMenuImg ? openMenuImg : closeMenuImg;
$(this).parent().children("h3").children("a.toggle").children("img").attr('src', targetImg);
});
});
Here is an example of how to do it with an arrow instead of a +
and -
. This uses jQuery
to change classes on the element, and CSS
to style the arrow.
$(".toggleHide").click(function() {_x000D_
$(".elementToHide").slideToggle("fast");_x000D_
$(this).find("i").toggleClass("down up");_x000D_
});
_x000D_
i {_x000D_
border: solid black;_x000D_
border-width: 0 5px 5px 0;_x000D_
display: inline-block;_x000D_
padding: 5px;_x000D_
-webkit-transition-duration: 1s;_x000D_
/* Safari */_x000D_
transition-duration: 1s;_x000D_
}_x000D_
_x000D_
.up {_x000D_
transform: rotate(-135deg);_x000D_
-webkit-transform: rotate(-135deg);_x000D_
}_x000D_
_x000D_
.down {_x000D_
transform: rotate(45deg);_x000D_
-webkit-transform: rotate(45deg);_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
_x000D_
<h3 class="toggleHide"><i class="down"></i></h3>_x000D_
<aside class="elementToHide">_x000D_
Content to hide_x000D_
</aside>
_x000D_
I would say the most elegant way is this:
<div class="toggle"></div>
<div class="content">...</div>
then css:
.toggle{
display:inline-block;
height:48px;
width:48px; background:url("http://icons.iconarchive.com/icons/pixelmixer/basic/48/plus-icon.png");
}
.toggle.expanded{
background:url("http://cdn2.iconfinder.com/data/icons/onebit/PNG/onebit_32.png");
}
and js:
$(document).ready(function(){
var $content = $(".content").hide();
$(".toggle").on("click", function(e){
$(this).toggleClass("expanded");
$content.slideToggle();
});
});
Toggle the text Show
and Hide
and move your backgroundPosition
Y axis
$(function(){ // DOM READY shorthand
$(".slidingDiv").hide();
$('.show_hide').click(function( e ){
// e.preventDefault(); // If you use anchors
var SH = this.SH^=1; // "Simple toggler"
$(this).text(SH?'Hide':'Show')
.css({backgroundPosition:'0 '+ (SH?-18:0) +'px'})
.next(".slidingDiv").slideToggle();
});
});
CSS:
.show_hide{
background:url(plusminus.png) no-repeat;
padding-left:20px;
}
Source: Stackoverflow.com