原生Js實(shí)現(xiàn)的畫(huà)廊功能
原生Js實(shí)現(xiàn)畫(huà)廊功能,點(diǎn)擊圖片,在下方出現(xiàn)相應(yīng)放大圖片。給a標(biāo)簽綁定onclick點(diǎn)擊事件。這里上方的小圖和下方將要展示大圖,都是同一張圖片,只是上下兩個(gè)img的style中設(shè)置了不同的width和heigth。(如果不想設(shè)置width、height,另一種方法就是將a標(biāo)簽里src的圖片存成大圖,img展示的是小圖。)通過(guò)Js點(diǎn)擊事件結(jié)合css實(shí)現(xiàn)大圖顯示或隱藏,切換圖片
具體如下圖,代碼附上
第一種
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
img{
width: 200px;
height: 100px;
}
#showimg{
width: 500px;
height: 240px;
/* background-color: pink;*/
}
.hide{
display: none;
}
.show{
display: block;
}
</style>
</head>
<body>
<div id = "imagegallery">
<a href="../../imgs/1.jpg" rel="external nofollow" title="pic1">
<img src="../../imgs/1.jpg" alt="1">
</a>
<a href="../../imgs/2.jpg" rel="external nofollow" title="pic2">
<img src="../../imgs/2.jpg" alt="2">
</a>
<a href="../../imgs/3.jpg" rel="external nofollow" title="pic3">
<img src="../../imgs/3.jpg" alt="3">
</a>
<a href="../../imgs/4.jpg" rel="external nofollow" title="pic4">
<img src="../../imgs/4.jpg" alt="4">
</a>
</div>
<!-- 清除浮動(dòng)的 -->
<div style="clear: both"></div>
<!--利用空白的一個(gè)圖占一個(gè)位置 -->
<!-- <img id="image" src="" alt="" width="450px"> -->
<img id="showimg" src="" class="hide" alt="">
<p id="desc"></p>
<script>
var imagegallery = document.getElementById("imagegallery");
var link = document.getElementsByTagName("a");
var showimg = document.getElementById("showimg");
var desc = document.getElementById("desc");
//for循環(huán)內(nèi)部添加的綁定事件,在觸發(fā)時(shí),所有的批量添加的事件已經(jīng)成功,觸發(fā)事件時(shí)都是在循環(huán)結(jié)束之后
//批量綁定的事件的事件函數(shù)內(nèi)部如果有變量i,要注意,函數(shù)執(zhí)行時(shí)已經(jīng)是在循環(huán)結(jié)束后
//循環(huán)內(nèi)部定義的變量是一個(gè)全局變量,在循環(huán)后執(zhí)行的i變量的值是i跳出循環(huán)時(shí)的值 image.src = links[i].href;
// for(var i = 0;i < link.length;i++){
// link[i].onclick = function(){
// // alert("123");
// // 更改image內(nèi)部的src屬性值
// showimg.src = link[i].href;
// // 更改desc內(nèi)部文字描述
// return false;//取消a標(biāo)簽的默認(rèn)跳轉(zhuǎn)
// }
// }
for(var i = 0;i < link.length;i++){
link[i].onclick = function(){
// alert("123");
// 更改image內(nèi)部的src屬性值
showimg.src = this.href;//this. 關(guān)鍵字指代的是觸發(fā)事件的真正事件源
//更改img顯示
showimg.className = "show";
// 更改desc內(nèi)部文字描述
desc.innerText = this.title;
return false;//取消a標(biāo)簽的默認(rèn)跳轉(zhuǎn)
}
}
</script>
</body>
</html>

第二種
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>簡(jiǎn)易燈箱畫(huà)廊設(shè)計(jì)</title>
<style>
*{
margin: 0;
}
#total{
width:100%;
background-color: green;
height:1000px;
}
#fpic{
margin-left: 15%;
}
.pic{
margin : 50px;
width: 100px;
height: 100px;
}
#show-bigger{
margin: auto;
width: 600px;
height: 450px;
background-color: red;
}
</style>
</head>
<body >
<div id="total">
<h3 style="color: white;text-align:center;">簡(jiǎn)易畫(huà)廊設(shè)計(jì)</h3>
<hr color="red">
<img class="pic" id="fpic"src="trees/t1.jpg" onclick="myfunction(this)" tabIndex=1 onblur="myfunction1(this)">
<img class="pic" src="trees/t2.jpg" onclick="myfunction(this)" tabIndex=1 onblur="myfunction1(this)">
<img class="pic" src="trees/t3.jpg" onclick="myfunction(this)" tabIndex=1 onblur="myfunction1(this)">
<img class="pic" src="trees/t4.jpg" onclick="myfunction(this)" tabIndex=1 onblur="myfunction1(this)">
<img class="pic" src="trees/t5.jpg" onclick="myfunction(this)" tabIndex=1 onblur="myfunction1(this)">
<img class="pic" src="trees/t6.jpg" onclick="myfunction(this)" tabIndex=1 onblur="myfunction1(this)">
<div id="show-bigger"><img id="spic" src="trees/t1.jpg" style="width: 100%;height: 100%"></div>
</div>
</body>
<script type="text/javascript">
function myfunction(x){ //改變展示框的圖片和被選中展示圖片的邊框
document.getElementById("spic").src=x.src;
x.style.borderBottom="5px solid red";
}
function myfunction1(x){ //消除未選擇的邊框?qū)傩?
x.style.border="none";
}
</script>
</html>

以上就是原生Js實(shí)現(xiàn)的畫(huà)廊功能的詳細(xì)內(nèi)容,更多關(guān)于Js實(shí)現(xiàn)畫(huà)廊功能的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
js操作css屬性實(shí)現(xiàn)div層展開(kāi)關(guān)閉效果的方法
這篇文章主要介紹了js操作css屬性實(shí)現(xiàn)div層展開(kāi)關(guān)閉效果的方法,涉及javaScript操作css樣式實(shí)現(xiàn)div彈出層的效果,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05
利用javascript實(shí)現(xiàn)禁用網(wǎng)頁(yè)上所有文本框,下拉菜單,多行文本域
這篇文章主要介紹了利用javascript實(shí)現(xiàn)禁用網(wǎng)頁(yè)上所有文本框,下拉菜單,多行文本域。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12
JavaScript實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
微信小程序使用map組件實(shí)現(xiàn)檢索(定位位置)周邊的POI功能示例
這篇文章主要介紹了微信小程序使用map組件實(shí)現(xiàn)檢索(定位位置)周邊的POI功能,涉及微信小程序基于map組件與高德地圖PAI接口的定位操作相關(guān)使用技巧,需要的朋友可以參考下2019-01-01
JavaScript canvas實(shí)現(xiàn)刮刮樂(lè)案例
這篇文章主要為大家詳細(xì)介紹了JavaScript canvas實(shí)現(xiàn)刮刮樂(lè)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
JavaScript錯(cuò)誤處理操作實(shí)例詳解
這篇文章主要介紹了JavaScript錯(cuò)誤處理操作,結(jié)合實(shí)例形式分析了javascript常見(jiàn)的錯(cuò)誤類(lèi)型、錯(cuò)誤處理語(yǔ)句以及相關(guān)使用技巧,需要的朋友可以參考下2019-01-01
JS實(shí)現(xiàn)倒計(jì)時(shí)圖文效果
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)倒計(jì)時(shí)圖文效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

