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
this works:
http://jsfiddle.net/UhEut/
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).
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);
});
});
Try something like this
jQuery
$('#toggle_icon').toggle(function() {
$('#toggle_icon').text('-');
$('#toggle_text').slideToggle();
}, function() {
$('#toggle_icon').text('+');
$('#toggle_text').slideToggle();
});
HTML
<a href="#" id="toggle_icon">+</a>
<div id="toggle_text" style="display: none">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
</div>
DEMO
Toggle the text Show
and Hide
and move your backgroundPosition
Y axis
LIVE DEMO
$(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;
}
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() {
$(".elementToHide").slideToggle("fast");
$(this).find("i").toggleClass("down up");
});
i {
border: solid black;
border-width: 0 5px 5px 0;
display: inline-block;
padding: 5px;
-webkit-transition-duration: 1s;
/* Safari */
transition-duration: 1s;
}
.up {
transform: rotate(-135deg);
-webkit-transform: rotate(-135deg);
}
.down {
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3 class="toggleHide"><i class="down"></i></h3>
<aside class="elementToHide">
Content to hide
</aside>
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();
});
});
FIDDLE