js动态切换图片的方法
网络编程 2021-07-04 21:47www.168986.cn编程入门
这篇文章主要介绍了js动态切换图片的方法,包含完整的css文件与js文件实现技巧,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了js动态切换图片的方法。分享给大家供大家参考。具体实现方法如下:
index.css文件如下:
代码如下:
* {
margin: 0px;padding: 0px;
}
body {
width: 632px;
/*background-color: blue;*/
margin: 0 auto;
}
#imgsCom {
background-color: yellow;
/*相对定位,为了下层可以使用绝对定位时以本div的原点为原点*/
position: relative;
}
#ulnav{
list-style-type: none;
position: absolute;
/*使用以imgsCom为原点来绝对定位到右下角*/
bottom: 0px;
right: 0px;
}
#ulnav li{
list-style-type: none;
float: left;
background-color: black;
color: white;
margin-right: 5px;
width: 20px;
height: 20px;
line-height: 20px;
text-align: center;
cursor: pointer;
}
margin: 0px;padding: 0px;
}
body {
width: 632px;
/*background-color: blue;*/
margin: 0 auto;
}
#imgsCom {
background-color: yellow;
/*相对定位,为了下层可以使用绝对定位时以本div的原点为原点*/
position: relative;
}
#ulnav{
list-style-type: none;
position: absolute;
/*使用以imgsCom为原点来绝对定位到右下角*/
bottom: 0px;
right: 0px;
}
#ulnav li{
list-style-type: none;
float: left;
background-color: black;
color: white;
margin-right: 5px;
width: 20px;
height: 20px;
line-height: 20px;
text-align: center;
cursor: pointer;
}
index.html如下:
代码如下:
<!DOCTYPE html>
<html xmlns="http://.w3./1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js,css动态切换图片</title>
<link href="css/index.css" rel="stylesheet" />
<script type="text/javascript">
function gel(id) {
return document.getElementById(id);
}
function clearLiBg() {
var mylis = gel("ulnav").childNodes;
for (var i = 0; i < mylis.length; i++) {
if (mylis[i].nodeType == 1) {
mylis[i].style.backgroundColor = "black";
}
}
}
window.onload = function() {
//给三个li都指定一个属性
var lis = gel("ulnav").childNodes;
for (var i = 0; i < lis.length; i++) {
if (lis[i].nodeType == 1) {
lis[i].onclick = function () {
//更换图片
gel("myimg").setAttribute("src", "images/" + this.innerHTML + ".png");
//所有LI颜色复原
clearLiBg();
//更换LI背景标签颜色
this.style.backgroundColor = "silver";
};
}
}
};
</script>
</head>
<body>
<div id="imgsCom" style="width: 632px; height: 412px;">
<img src="images/1.png" id="myimg" style="width: 632px; height: 412px; " />
<ul id="ulnav">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
</body>
</html>
<html xmlns="http://.w3./1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js,css动态切换图片</title>
<link href="css/index.css" rel="stylesheet" />
<script type="text/javascript">
function gel(id) {
return document.getElementById(id);
}
function clearLiBg() {
var mylis = gel("ulnav").childNodes;
for (var i = 0; i < mylis.length; i++) {
if (mylis[i].nodeType == 1) {
mylis[i].style.backgroundColor = "black";
}
}
}
window.onload = function() {
//给三个li都指定一个属性
var lis = gel("ulnav").childNodes;
for (var i = 0; i < lis.length; i++) {
if (lis[i].nodeType == 1) {
lis[i].onclick = function () {
//更换图片
gel("myimg").setAttribute("src", "images/" + this.innerHTML + ".png");
//所有LI颜色复原
clearLiBg();
//更换LI背景标签颜色
this.style.backgroundColor = "silver";
};
}
}
};
</script>
</head>
<body>
<div id="imgsCom" style="width: 632px; height: 412px;">
<img src="images/1.png" id="myimg" style="width: 632px; height: 412px; " />
<ul id="ulnav">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程