html中如何鼠标点击更换背景图片
2016-06-27 · 百度知道合伙人官方认证企业
html中鼠标点击更换背景图片的方法:
1、html代码:
<div id="menuWrapper" class="menuWrapper bg1">
<ul class="menu" id="menu">
<li class="bg1" style="background-position:0 0;">
<a id="bg1" href="#">迈瑞宝</a>
<ul class="sub1" style="background-position:0 0;">
<li><a href="#">报价:11.99-23.69万</a></li>
<li><a href="#">车身结构:三箱</a></li>
<li><a href="#">油耗:8.3-12.0L</a></li>
</ul>
</li>
<li class="bg1" style="background-position:-266px 0px;">
<a id="bg2" href="#">索纳塔8</a>
<ul class="sub2" style="background-position:-266px 0;">
<li><a href="#">报价:13.39-22.59万</a></li>
<li><a href="#">车身结构:三箱</a></li>
<li><a href="#">油耗:9.0-12.0L</a></li>
</ul>
</li>
<li class="last bg1" style="background-position:-532px 0px;">
<a id="bg3" href="#">K5</a>
<ul class="sub3" style="background-position:-266px 0;">
<li><a href="#">报价:10.88-25.58万</a></li>
<li><a href="#">车身结构:三箱</a></li>
<li><a href="#">油耗:8.4-13.0L</a></li>
</ul>
</li>
</ul>
</div>
2、css代码:
ul.menu > li > a{
float:left;
width:265px;
height:50px;
margin-top:450px;
text-align:center;
line-height:50px;
color:#ddd;
background-color:#333;
letter-spacing:1px;
cursor:pointer;
text-decoration:none;
text-shadow:0px 0px 1px #fff;
}
ul.menu > li ul{
list-style:none;
float:left;
margin-top:-180px;
width:100%;
height:110px;
padding-top:20px;
background-repeat:no-repeat;
background-color:transparent;
}
ul.menu > li ul li{
display:none;
}
ul.menu > li ul.sub1 {
background-image: url('../img/bg1sub.png');
}
ul.menu > li ul.sub2 {
background-image: url('../img/bg2sub.png');
}
ul.menu > li ul.sub3{
background-image:url(../img/bg3sub.png);
}
ul.menu > li ul li a{
color:#fff;
text-decoration:none;
line-height:30px;
margin-left:20px;
text-shadow:1px 1px 1px #444;
font-size:11px;
}
ul.menu > li ul li a:hover{
border-bottom:1px dotted #fff;
}
ul.menu > li ul.sub1 li{
display:block;
}
3、js代码:
if (!document.defaultView || !document.defaultView.getComputedStyle) { // IE6-IE8
var oldCurCSS = jQuery.curCSS;
jQuery.curCSS = function (elem, name, force) {
if (name === 'background-position') {
name = 'backgroundPosition';
}
if (name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[name]) {
return oldCurCSS.apply(this, arguments);
}
var style = elem.style;
if (!force && style && style[name]) {
return style[name];
}
return oldCurCSS(elem, 'backgroundPositionX', force) + ' ' + oldCurCSS(elem, 'backgroundPositionY', force);
};
}
var oldAnim = $.fn.animate;
$.fn.animate = function (prop) {
if ('background-position' in prop) {
prop.backgroundPosition = prop['background-position'];
delete prop['background-position'];
}
if ('backgroundPosition' in prop) {
prop.backgroundPosition = '(' + prop.backgroundPosition;
}
return oldAnim.apply(this, arguments);
};
function toArray(strg) {
strg = strg.replace(/left|top/g, '0px');
strg = strg.replace(/right|bottom/g, '100%');
strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g, "$1px$2");
var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
return [parseFloat(res[1], 10), res[2], parseFloat(res[3], 10), res[4]];
}
4、实现效果
2016-05-28 · 知道合伙人软件行家
点击更换背景图片要用到Javascript,建议使用最常用的JQuery来简单实现,鼠标点击时,修改body的background属性即可,代码可参考如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery换背景图片点击设置背景图片代码演示_大头网</title>
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<style type="text/css">
*{margin:0px;padding:0px;}
body{background:url('images/55.jpg');}
.but{width:50px;height:50px;background:url('images/rtop_1.png');display:block;position:fixed;top:0px;right:0px;}
.but:hover{background:url('images/rtop_2.png');}
#hf{width:100%;height:200px;background:rgba(0,0,0,0.5);display:none;}
#hf .con{width:1200px;height:200px;margin:0 auto;position:relative;}
#hf .con .left{position:absolute;top:70px;left:0px;cursor:pointer;}
#hf .con .right{position:absolute;top:70px;right:0px;cursor:pointer;}
#hf .con .scroll{width:1080px;height:200px;
overflow:hidden;margin:0 auto;position:relative;}
#hf .con .scroll .scrollCon{width:1000%;height:200px;position:absolute;left:0px;top:0px;}
.scroll .scrollCon ul li{list-style:none;width:240px;height:140px;border:3px solid #fff;float:left;margin-left:12px;margin-right:12px;margin-top:20px;cursor:pointer;}
</style>
</head>
<body>
<!-- 代码 开始 -->
<a href="#" class="but"></a>
<div id="hf">
<div class="con">
<img src="images/left.png" class="left"/>
<img src="images/right.png" class="right"/>
<div class="scroll">
<div class="scrollCon">
<ul>
<li><img src="images/99-1.jpg"/></li>
<li><img src="images/11-1.jpg"/></li>
<li><img src="images/55-1.jpg"/></li>
<li><img src="images/33-1.jpg"/></li>
<li><img src="images/44-1.jpg"/></li>
<li><img src="images/55-1.jpg"/></li>
<li><img src="images/99-1.jpg"/></li>
<li><img src="images/44-1.jpg"/></li>
<li><img src="images/55-1.jpg"/></li>
<li><img src="images/99-1.jpg"/></li>
<li><img src="images/11-1.jpg"/></li>
<li><img src="images/33-1.jpg"/></li>
</ul>
</div>
</div>
</div>
</div>
<script type="text/javascript">
<!-- 点击收缩换肤栏 -->
$(".but").click(function(){
$("#hf").slideToggle("slow");
});
<!-- 点击换body图 -->
$(".scrollCon ul li").click(function(){
var simg=$(this).find("img").attr("src");
var bimg=simg.replace(/-\d*/,''); //根据小图找到大图的名称
$("body").css("background","url("+bimg+")");//url("+bimg+"),添加 变量的方法
});
<!-- 点击左边按钮 -->
var click_num=0; //初始点击次数
$(".left").click(function(){
click_num++; //click_num+1
if(click_num>2){
click_num=0;
}
$(".scrollCon").animate({left:click_num*(-1080)},300);
});
$(".right").click(function(){
click_num--; //click_num+1
if(click_num<0){
click_num=2;
}
$(".scrollCon").animate({left:click_num*(-1080)},300);
});
</script>
<!-- 代码 结束 -->
<div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
<p>来源:<a href="http://www.datouwang.com/" target="_blank">大头网</a></p>
<p>转载请注明出处,此代码仅供学习交流,请勿用于商业用途。</p>
<p><script src="http://www.datouwang.com/img/js/demo_ad.js"></script><center style="display:none"><script src="http://www.datouwang.com/img/js/demo_tj.js"></script></center></p>
</div>
</body>
</html>
具体效果可以在以下网址查看
http://www.datouwang.com/uploads/demo/jiaoben/2015/jiaoben454/#
$(id).css("background-image",". .图. .");
})
广告 您可能关注的内容 |