Toggle css background color and image
Which expands and collapses on click. Now I want to toggle an image along with it, which is a css background element. I am not able to figure out how.
<script>
$(document).ready(function(){
$(".panel_head").click(function(){
//$(this).slideToggle("slow");
$(this).next('div').slideToggle('slow', function() {
});
var myEl = $(this);
var bgimg = myEl.css('background');
if( bgimg === #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat ) {
myEl.css({ 'background' : #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat });
} else {
myEl.css({ 'background' : #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat });
}
});
});
</script>
This is my css
.panel_head {
border-radius: 0 !important;
color: white;
font-size: 14pt;
background: #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat;
text-align: center;
cursor: pointer;
}
And this is my html
<div class="panel custom_panel">
<div class="panel-heading panel_head" id="panel-head1">
<span class="expand-collapse-text exp" >
Categories
</span>
</div>
<div class="panel-body" id="panel-body1">
<ul class="left_options list-unstyled">
<li> <input value="1" id="ca1" class="handleclick" type="checkbox" name="ca"><label for="ca1"><span></span>ALL(A-Z Listing)</label> </li>
<li> <input value="2" id="ca2" class="handleclick" type="checkbox" name="ca"><label for="ca2"><span></span>Health Check Up</label> </li>
<li> <input value="3" id="ca3" class="handleclick" type="checkbox" name="ca"><label for="ca3"><span></span>Treatment</label> </li>
<li> <input value="16" id="ca16" class="handleclick" type="checkbox" name="ca"><label for="ca16"><span></span> Procedure </label> </li>
</ul>
</div>
</div>
I am getting errors in my script.
用css代替js:
$(document).ready(function(){
$(".panel_head").click(function(){
$(this).next('div').slideToggle('slow', function() {});
$(this).toggleClass('active');
});
});
.panel_head {
border-radius: 0 !important;
color: white;
font-size: 14pt;
background: #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat;
text-align: center;
cursor: pointer;
}
.panel_head.active {
background: red url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow_1.png') 5% center no-repeat;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel custom_panel">
<div class="panel-heading panel_head" id="panel-head1">
<span class="expand-collapse-text exp">
Categories
</span>
</div>
<div class="panel-body" id="panel-body1">
<ul class="left_options list-unstyled">
<li><input value="1" id="ca1" class="handleclick" type="checkbox" name="ca"><label for="ca1"><span></span>ALL(A-Z Listing)</label></li>
<li><input value="2" id="ca2" class="handleclick" type="checkbox" name="ca"><label for="ca2"><span></span>Health Check Up</label></li>
<li><input value="3" id="ca3" class="handleclick" type="checkbox" name="ca"><label for="ca3"><span></span>Treatment</label></li>
<li><input value="16" id="ca16" class="handleclick" type="checkbox" name="ca"><label for="ca16"><span></span> Procedure </label></li>
</ul>
</div>
</div>
hey dude you just not workaround properly with the quotes( "" or '' ) thats why
for example i have given
if( bgimg === "#00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat" ) {
//your code
}
链接地址: http://www.djcxy.com/p/74104.html
上一篇: 迭代每个并包含在jquery中
下一篇: 切换CSS背景颜色和图像