JavaScript實(shí)現(xiàn)簡(jiǎn)單圖片切換
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)簡(jiǎn)單圖片切換的具體代碼,供大家參考,具體內(nèi)容如下
下邊給出幾種方法進(jìn)行圖片切換:
方法一 (小白專用款!簡(jiǎn)單易懂) 下邊附上代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>圖片切換2</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
#box{
border: 1px solid #ccc;
width: 450px;
height: 70px;
padding-top: 450px;
background: url("img/big_pic01.jpg") no-repeat;
}
#box ul li{
float: left;
padding-left: 10px;
}
</style>
</head>
<body>
<div id="box">
<ul>
<li id="item1">
<img src="img/pic01.webp">
</li>
</ul>
<ul>
<li id="item2">
<img src="img/pic02.webp">
</li>
</ul>
<ul>
<li id="item3">
<img src="img/pic03.webp">
</li>
</ul>
<ul>
<li id="item4">
<img src="img/pic04.webp">
</li>
</ul>
<ul>
<li id="item5">
<img src="img/pic05.webp">
</li>
</ul>
</div>
<script type="text/javascript">
// 初學(xué)者 小白 書寫方式
// 1.獲取事件源
var item1 = document.getElementById("item1");
var item2 = document.getElementById("item2");
var item3 = document.getElementById("item3");
var item4 = document.getElementById("item4");
var item5 = document.getElementById("item5");
var oBos = document.getElementById("box");
// 2.添加事件
item1.onmouseover = function (){
//當(dāng)鼠標(biāo)懸浮在相關(guān)id上時(shí),圖片指向路徑進(jìn)行改變
oBos.style.background = "url('img/big_pic01.jpg') no-repeat";
}
item2.onmouseover = function (){
oBos.style.background = "url('img/big_pic02.jpg') no-repeat";
}
item3.onmouseover = function (){
oBos.style.background = "url('img/big_pic03.jpg') no-repeat";
}
item4.onmouseover = function (){
oBos.style.background = "url('img/big_pic04.jpg') no-repeat";
}
item5.onmouseover = function (){
oBos.style.background = "url('img/big_pic05.jpg') no-repeat";
}
</script>
</body>
</html>
上邊的JS部分代碼可能比較麻煩,容易造成代碼冗余。
那么我們進(jìn)行修改下后,來(lái)看看方法二:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>圖片切換2</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
#box{
border: 1px solid #ccc;
width: 450px;
height: 70px;
padding-top: 450px;
background: url("img/big_pic01.jpg") no-repeat;
}
#box ul li{
float: left;
padding-left: 10px;
}
</style>
</head>
<body>
<div id="box">
<ul>
<li id="item1">
<img src="img/pic01.webp">
</li>
</ul>
<ul>
<li id="item2">
<img src="img/pic02.webp">
</li>
</ul>
<ul>
<li id="item3">
<img src="img/pic03.webp">
</li>
</ul>
<ul>
<li id="item4">
<img src="img/pic04.webp">
</li>
</ul>
<ul>
<li id="item5">
<img src="img/pic05.webp">
</li>
</ul>
</div>
<script type="text/javascript">
// 1.獲取事件源 這樣獲取事件源省去了大量的var的定義的過(guò)程
function $(id){
return typeof id === "string"?document.getElementById(id):null;
}
// 改變背景圖 liId是指向的id imgSrc是將背景圖傳入的參數(shù)
function changebgcImg(liId,imgSrc){
// 2.添加事件
$(liId).onmouseover = function (){
// 3.改變背景圖
$("box").style.background = imgSrc;
}
}
changebgcImg("item1",'url("img/big_pic01.jpg") no-repeat');
changebgcImg("item2",'url("img/big_pic02.jpg") no-repeat');
changebgcImg("item3",'url("img/big_pic03.jpg") no-repeat');
changebgcImg("item4",'url("img/big_pic04.jpg") no-repeat');
changebgcImg("item5",'url("img/big_pic05.jpg") no-repeat');
</script>
</body>
</html>
可以看到,方法二比方法一所用JS代碼要少。
接著上邊的進(jìn)行修改,我們可以來(lái)看看方法三:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>圖片切換完整版</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
#box{
border: 1px solid #ccc;
width: 450px;
height: 70px;
padding-top: 450px;
background: url("img/big_pic01.jpg") no-repeat;
}
#box ul li{
float: left;
padding-left: 10px;
}
</style>
</head>
<body>
<div id="box">
<ul>
<li class="item">
<img src="img/pic01.webp">
</li>
</ul>
<ul>
<li class="item">
<img src="img/pic02.webp">
</li>
</ul>
<ul>
<li class="item">
<img src="img/pic03.webp">
</li>
</ul>
<ul>
<li class="item">
<img src="img/pic04.webp">
</li>
</ul>
<ul>
<li class="item">
<img src="img/pic05.webp">
</li>
</ul>
</div>
<script type="text/javascript">
// 1.獲取事件源
function $(id){
return typeof id === 'string'? document.getElementById(id):null;
}
// 獲取下邊的所有名為item的li標(biāo)簽
var items = document.getElementsByClassName("item");
// console.log(items);
for (var i=0;i<items.length;i++){
var item = items[i];
item.index = i+1;
items[i].onmouseover = function (){
$("box").style.background = `url("img/big_pic0${this.index}.jpg") no-repeat`
// 不可以直接設(shè)置${i}而要重新定義個(gè)變量item是因?yàn)樵趂unction中調(diào)用的i是全局變量,i在for循環(huán)后會(huì)一直指向5
// $("box").style.background = `url("img/big_pic0${i}.jpg") no-repeat`
}
}
</script>
</body>
</html>
三種方式,都可以實(shí)現(xiàn)圖片切換效果(第一種如果圖片數(shù)目多,要展示的圖片也不一樣多的話不推薦使用),圖片切換效果如下:

我還有篇博客也是進(jìn)行圖片切換效果的,所用方式與此三種有些區(qū)別,可以進(jìn)行參考,就先不進(jìn)行合并了:JavaScript實(shí)現(xiàn)淘寶商品圖切換效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 最簡(jiǎn)單的js圖片切換效果實(shí)現(xiàn)代碼
- 用html+css+js實(shí)現(xiàn)的一個(gè)簡(jiǎn)單的圖片切換特效
- 純js實(shí)現(xiàn)背景圖片切換效果代碼
- 簡(jiǎn)單的實(shí)現(xiàn)點(diǎn)擊箭頭圖片切換的js代碼
- css圖片切換效果代碼[不用js]
- 純js無(wú)flash仿搜狐女人頻道FLASH圖片切換效果代碼
- javascript實(shí)現(xiàn)圖片切換的幻燈片效果源代碼
- JavaScript實(shí)現(xiàn)圖片切換效果
- js鼠標(biāo)點(diǎn)擊圖片切換效果代碼分享
- JS實(shí)現(xiàn)圖片切換效果
相關(guān)文章
BootStrap 下拉菜單點(diǎn)擊之后不會(huì)出現(xiàn)下拉菜單(下拉菜單不彈出)的解決方案
最近學(xué)到Bootstrap下拉菜單,學(xué)懂了教程內(nèi)容之后自己敲一個(gè)點(diǎn)擊按鈕底下彈出下拉菜單的小demo,寫完代碼發(fā)現(xiàn)運(yùn)行之后點(diǎn)擊按鈕沒(méi)反應(yīng),下拉菜單彈不出來(lái),下面給大家分享下解決方案2016-12-12
[js高手之路]單例模式實(shí)現(xiàn)模態(tài)框的示例
下面小編就為大家?guī)?lái)一篇[js高手之路]單例模式實(shí)現(xiàn)模態(tài)框的示例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
mui框架 頁(yè)面無(wú)法滾動(dòng)的解決方法(推薦)
下面小編就為大家分享一篇mui框架 頁(yè)面無(wú)法滾動(dòng)的解決方法(推薦),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
JavaScript中的Primitive對(duì)象封裝介紹
這篇文章主要介紹了JavaScript中的Primitive對(duì)象封裝介紹,本文著重講解封裝過(guò)程,然后給出示例代碼,需要的朋友可以參考下2014-12-12
js實(shí)現(xiàn)每日自動(dòng)換一張圖片的方法
這篇文章主要介紹了js實(shí)現(xiàn)每日自動(dòng)換一張圖片的方法,涉及javascript操作圖片與日期的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05
解決option標(biāo)簽selected="selected"屬性失效的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決option標(biāo)簽selected="selected"屬性失效的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望對(duì)大家有所幫助2017-11-11

