js動態(tài)切換圖片的方法
更新時間:2015年01月20日 09:31:22 投稿:shichen2014
這篇文章主要介紹了js動態(tài)切換圖片的方法,包含完整的css文件與js文件實現技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了js動態(tài)切換圖片的方法。分享給大家供大家參考。具體實現方法如下:
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://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js,css動態(tài)切換圖片</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://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js,css動態(tài)切換圖片</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程序設計有所幫助。
相關文章
編寫跨瀏覽器的javascript代碼必備[js多瀏覽器兼容寫法]
下面比較了幾種瀏覽器之間的差異,在寫javascript代碼時 要時刻注意這些差異2008-10-10
Bootstrap簡單實用的表單驗證插件BootstrapValidator用法實例詳解
這篇文章主要介紹了Bootstrap簡單實用的表單驗證插件BootstrapValidator用法,結合實例形式詳細分析了Bootstrap表單驗證插件BootstrapValidator基本功能、原理、用法及操作注意事項,需要的朋友可以參考下2020-03-03
js模仿html5 placeholder適應于不支持的瀏覽器
html5原生支持placeholder,對于不支持的瀏覽器(ie)可用js模擬實現,不要走開,接下來為您詳細介紹實現方法2013-01-01
分享XmlHttpRequest調用Webservice的一點心得
因為項目需要,以后前端、手機客戶端調用ASP.NET的Webservice來獲取信息.所以這段時間開始看Webservice,試著通過XmlHttpRequest調用Webservice,過程中碰到不少問題,也有不少的收獲2012-07-07

