HTML怎么设置点击一张图片换一张图片

html-css014

HTML怎么设置点击一张图片换一张图片,第1张

需要准备的材料分别有:电脑、浏览器、html编辑器。

1、首先,打开html编辑器,新建html文件,例如:index.html,编写问题基础代码。

2、在index.html中的<script>标签,输入js代码:

function fun() {

var a = Math.random() * 5

$('img').attr('src', 'image' + a + '.jpg')

}

3、浏览器运行index.html页面,此时点击图片会换一张图片。

1、首先输入代码:

<div class="wrapper">

<div id="focus">

<ul>

<li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/01.jpg" alt="QQ商城焦点图效果下载" /></a></li>

2、然后输入代码:

<li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/02.jpg" alt="QQ商城焦点图效果教程" /></a></li>

<li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/03.jpg" alt="jquery商城焦点图效果" /></a></li>

<li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/04.jpg" alt="jquery商城焦点图代码" /></a></li>

3、然后再输入代码:

<li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/05.jpg" alt="jquery商城焦点图源码" /></a></li>

</ul>

</div>

</div><!-- wrapper end -->

</body>

4、然后就完成了。

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>H5course</title>

<link rel="stylesheet" href="css/reset.css">

<style>

.imgview {

width: 255px

height: 402px

margin: 50px auto

border: 10px solid red

overflow: hidden

}

.imgview img {

width: 255px

height: 402px

}

.imgview-nav {

width: 255px

margin: 10px auto

border: 1px solid red

list-style-type: none

}

.imgview-nav li {

float: left

width: 15px

height: 15px

border: 1px solid blue

margin: 10px

}

span {

display: inline-block

width: 50px

height: 50px

background: #CFC

border-radius: 50%

}

</style>

</head>

<body>

<div class="wrap">

<div class="imgview" id="imgView">

<table cellspacing="0" cellpadding="0">

<tr>

<td><img src="img/1.jpg" alt=""></td>

<td><img src="img/2.jpg" alt=""></td>

<td><img src="img/3.jpg" alt=""></td>

<td><img src="img/4.jpg" alt=""></td>

<td><img src="img/5.jpg" alt=""></td>

<td><img src="img/6.jpg" alt=""></td>

</tr>

</table>

</div>

<ul class="imgview-nav" id="imgviewNav">

<li>1</li>

<li>2</li>

<li>3</li>

<li>4</li>

<li>5</li>

<li>6</li>

</ul>

<span>左边</span>

<span>右边</span>

</div>

<script>

var imgView = document.getElementById('imgView'),

imgWidth = imgView.getElementsByTagName('img'),

btn = document.getElementsByTagName('li'),

len = btn.length,

imgNum = 0,

endPos = 0,

timer = null,

timer2 = null,

conSpan = document.getElementsByTagName('span')

conSpan[0].onclick = function (){

if (timer) {

clearInterval(timer)

}

if (timer2) {

clearInterval(timer2)

}

imgNum--

if (imgNum <0 ) {

imgNum = len

}

timer = setInterval(move, 16)

timer2 = setInterval(autoMove, 3000)

}

conSpan[1].onclick = function (){

if (timer) {

clearInterval(timer)

}

if (timer2) {

clearInterval(timer2)

}

imgNum++

if (imgNum == len ) {

imgNum = 0

}

timer = setInterval(move, 16)

timer2 = setInterval(autoMove, 3000)

}

//绑定事件。添加移动效果

for (var i = 0i <leni++) {

btn[i].index = i

btn[i].onmouseover = function(){

clearInterval(timer2)

imgNum = this.index

if (timer) {

clearInterval(timer)

}

timer = setInterval(move, 16)

}

btn[i].onmouseout = function(){

timer2 = setInterval(autoMove, 2000)

}

}

//图片缓动效果,原理是保持步数不变,改变距离的同时改变速度

function move(){

var starPos = imgView.scrollLeft,

speed

endPos = imgNum * imgWidth[0].offsetWidth

speed = (endPos - starPos) / 20

if (speed == 0) {

clearInterval(timer)

} else if(speed >0){

speed = Math.ceil(speed)

} else {

speed = Math.floor(speed)

}

imgView.scrollLeft = starPos + speed

}

//控制图片的自动滚动,通过控制索引值来控制图片的自动滚动

function autoMove(){

imgNum++

if(imgNum == len) {

imgNum = 0

}

timer = setInterval(move, 20)

}timer2 = setInterval(autoMove, 2000)

</script>

</body>

</html>