如何使用js来控制图片的css样式?

html-css014

如何使用js来控制图片的css样式?,第1张

这个可以用CSS实现就好了。不必要使用JS

图片放大效果的话,建议用一个额外的 div层放置放大后的图片,这样布局就不会乱了。鼠标放上去就显示这个层。

.img2{

/*这里写你的所有图片都会用到的样式*/

}

.img2:first-child, .img2:last-child{

/*这里写第一个图片和最后一个图片会用到的样式*/

}

.img1:hover .showBigImg{

display:block

}

对于这个大图的div层的显示,可以稍加js控制,这样就可以所有图片共用一个div层来放置大图。

希望能帮助到你

[HTML部分]

<div id="tbody">

<span class="txt_1">横向的JS相册效果</span>

<div id="mainbody">

<img src="images/01.jpg" alt="横向JS相册" width="640" height="400" id="mainphoto" rel="images/01.jpg" name="#" />

</div>

<img src="images/goleft.gif" width="11" height="56" id="goleft" />

<img src="images/goright.gif" width="11" height="56" id="goright" />

<div id="photos">

<div id="showArea">

<!--src: 缩略图地址 rel: 大图地址 name: 网址 -->

<img src="images/01.jpg" alt="横向JS相册" width="80" height="50" rel="images/01.jpg" name="#" />

<img src="images/02.jpg" alt="横向JS相册" width="80" height="50" rel="images/02.jpg" name="#" />

<img src="images/01.jpg" alt="横向JS相册" width="80" height="50" rel="images/01.jpg" name="#" />

<img src="images/01.jpg" alt="横向JS相册" width="80" height="50" rel="images/01.jpg" name="#" />

<img src="images/02.jpg" alt="横向JS相册" width="80" height="50" rel="images/02.jpg" name="#" />

<img src="images/01.jpg" alt="横向JS相册" width="80" height="50" rel="images/01.jpg" name="#" />

<img src="images/01.jpg" alt="横向JS相册" width="80" height="50" rel="images/01.jpg" name="#" />

<img src="images/02.jpg" alt="横向JS相册" width="80" height="50" rel="images/02.jpg" name="#" />

<img src="images/01.jpg" alt="横向JS相册" width="80" height="50" rel="images/01.jpg" name="#" />

<img src="images/01.jpg" alt="横向JS相册" width="80" height="50" rel="images/01.jpg" name="#" />

<img src="images/02.jpg" alt="横向JS相册" width="80" height="50" rel="images/02.jpg" name="#" />

<img src="images/01.jpg" alt="横向JS相册" width="80" height="50" rel="images/01.jpg" name="#" />

<img src="images/01.jpg" alt="横向JS相册" width="80" height="50" rel="images/01.jpg" name="#" />

<img src="images/02.jpg" alt="横向JS相册" width="80" height="50" rel="images/02.jpg" name="#" />

<img src="images/01.jpg" alt="横向JS相册" width="80" height="50" rel="images/01.jpg" name="#" />

</div>

</div>

</div>

[CSS部分]

body {background:#000margin:0font:12px Verdanatext-align:center}

#tbody {width:650pxmargin:20px autotext-align:left}

#mainbody {width:640pxmargin:5pxborder:1px solid #222padding:1px}

#mainphoto {cursor:pointerdisplay:block}

#goleft {float:leftclear:leftmargin:6px 5px 0 3px}

#goright {float:rightclear:rightmargin:6px 3px 0 5px}

#photos {width:610pxheight:54pxline-height:54pxborder:1px solid #222margin:10px 0overflow:hidden}

#showArea img {display:blockfloat:leftmargin:1px 0cursor:pointerborder:1px solid #222}.txt_1 {font:bold 24px Verdana, Tahomacolor:#fff}

[JS部分]

<script language="javascript" type="text/javascript">

var browse = window.navigator.appName.toLowerCase()

var MyMar

var speed = 1//速度,越大越慢

var spec = 1//每次滚动的间距, 越大滚动越快

var minOpa = 90//滤镜最小值

var maxOpa = 100//滤镜最大值

var spa = 2//缩略图区域补充数值

var w = 0

spec = (browse.indexOf("microsoft") >-1) ? spec : ((browse.indexOf("opera") >-1) ? spec*10 : spec*20)

function $(e) {return document.getElementById(e)}

function goleft() {$('photos').scrollLeft -= spec}

function goright() {$('photos').scrollLeft += spec}

function setOpacity(e, n) {

if (browse.indexOf("microsoft") >-1) e.style.filter = 'alpha(opacity=' + n + ')'

else e.style.opacity = n/100

}

$('goleft').style.cursor = 'pointer'

$('goright').style.cursor = 'pointer'

$('mainphoto').onmouseover = function() {setOpacity(this, maxOpa)}

$('mainphoto').onmouseout = function() {setOpacity(this, minOpa)}

$('mainphoto').onclick = function() {location = this.getAttribute('name')}

$('goleft').onmouseover = function() {this.src = 'images/goleft2.gif'MyMar=setInterval(goleft, speed)}

$('goleft').onmouseout = function() {this.src = 'images/goleft.gif'clearInterval(MyMar)}

$('goright').onmouseover = function() {this.src = 'images/goright2.gif'MyMar=setInterval(goright,speed)}

$('goright').onmouseout = function() {this.src = 'images/goright.gif'clearInterval(MyMar)}

window.onload = function() {

setOpacity($('mainphoto'), minOpa)

var rHtml = ''

var p = $('showArea').getElementsByTagName('img')

for (var i=0i<p.lengthi++) {

w += parseInt(p[i].getAttribute('width')) + spa

setOpacity(p[i], minOpa)

p[i].onclick = function() {location = this.getAttribute('name')}

p[i].onmouseover = function() {

setOpacity(this, maxOpa)

$('mainphoto').src = this.getAttribute('rel')

$('mainphoto').setAttribute('name', this.getAttribute('name'))

setOpacity($('mainphoto'), maxOpa)

}

p[i].onmouseout = function() {

setOpacity(this, minOpa)

setOpacity($('mainphoto'), minOpa)

}

rHtml += '<img src="' + p[i].getAttribute('rel') + '" width="0" height="0" alt="" />'

}

$('showArea').style.width = parseInt(w) + 'px'

var rLoad = document.createElement("div")

$('photos').appendChild(rLoad)

rLoad.style.width = "1px"

rLoad.style.height = "1px"

rLoad.style.overflow = "hidden"

rLoad.innerHTML = rHtml

}

</script>