利用javascript怎么實現一個圖片放大功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

具體內容如下
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
body {
font-family: "Helvetica", "Arial", serif;
color: #333;
background-color: #ccc;
margin: 1em 10%;
}
h2 {
color: #333;
background-color: transparent;
}
a {
color: #c60;
background-color: transparent;
font-weight: bold;
text-decoration: none;
}
ul {
padding: 0;
}
li {
float: left;
padding: 1em;
list-style: none;
}
#imagegallery {
list-style: none;
}
#imagegallery li {
margin: 0px 20px 20px 0px;
padding: 0px;
display: inline;
}
#imagegallery li a img {
border: 0;
}
</style>
</head>
<body>
<h3>
美女畫廊
</h3>
<ul id="imagegallery">
<li>
<a href="images/1.jpg" rel="external nofollow" title="美女A">
<img src="images/1-small.jpg" width="100" alt="美女1"/>
</a>
</li>
<li><a href="images/2.jpg" rel="external nofollow" title="美女B">
<img src="images/2-small.jpg" width="100" alt="美女2"/>
</a></li>
<li><a href="images/3.jpg" rel="external nofollow" title="美女C">
<img src="images/3-small.jpg" width="100" alt="美女3"/>
</a></li>
<li><a href="images/4.jpg" rel="external nofollow" title="美女D">
<img src="images/4-small.jpg" width="100" alt="美女4"/>
</a></li>
</ul>
<div ></div>
<!--顯示大圖的-->
<img id="image" src="images/placeholder.png" alt="" width="450"/>
<p id="des">選擇一個圖片</p>
<script>
// 獲取ul元素對象
var imagegallery = document.getElementById("imagegallery")
// 獲取a元素 數組
var a = imagegallery.getElementsByTagName("a")
// 獲取大圖元素對象
var image = document.getElementById("image")
// 獲取p標簽
var des = document.getElementById("des")
// console.log(imagegallery,a)
// 遍歷數組元素為每一個a鏈接注冊點擊事件
for(var i = 0; i < a.length; i++){
a[i].onclick = function(){
// 將a鏈接中的href中的值賦值給大圖的src屬性
image.src = this.href
// 將a鏈接中的title的值作為內容賦值給p標簽
des.innerHTML = this.title
return false
}
}
//點擊a標簽,把a標簽中的href的屬性值給id為image的src屬性
//把a的title屬性的值給id為des的p標簽賦值
//阻止超鏈接默認的跳轉
// return false;
</script>
</body>
</html>看完上述內容,你們掌握利用javascript怎么實現一個圖片放大功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!
文章標題:利用javascript怎么實現一個圖片放大功能-創新互聯
標題路徑:http://www.yijiale78.com/article44/dicdhe.html
成都網站建設公司_創新互聯,為您提供云服務器、面包屑導航、小程序開發、ChatGPT、搜索引擎優化、自適應網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯