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程序设计有所帮助。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指