html中如何鼠标点击更换背景图片

html中如何鼠标点击更换背景图片,第1张

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、实现效果

图中所显示的是html5的 input color类型,在js中以 元素.value 来获取颜色 通过 元素.onchange来实现实时修改背景颜色。

<!--HTML代码-->

<input type="color" name = "colorselect">

//js代码

var color = document.getElementsByName("colorselect")[0]//获取该元素

color.onchange = function(){document.body.bgColor = color.value}//onchange事件实现实时更换

稍等上代码!

你好我写的例子是用jquery来写的,引用的网上的地址,你可以直接用。

<!DOCTYPE html>

<html>

<head>

<meta charset='utf-8' />

<title>JS Bin</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<style>

  .box1{

    width: 698px

    height: 258px

    border: 1px solid #ccc

    background: url(http://www.snowinmay.net/6rooms/html/img/gsh/gsh_banner3.jpg) no-repeat

  }

  .changeBg{

    background: url(http://www.snowinmay.net/6rooms/html/img/gsh/gsh_banner1.jpg) no-repeat

  }

</style>

</head>

<body>

  <div class="box1">

  

  </div>

<script>

$(function(){

  $('.box1').toggle(function() {

    $(this).addClass('changeBg')

  }, function() {

     $(this).removeClass('changeBg')

  })

  

})

</script>

</body>

</html>

希望能帮你,有问题可以追问!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6300234.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-20
下一篇 2023-03-20

发表评论

登录后才能评论

评论列表(0条)

保存