JavaScript实现图片高亮展示效果
作者:纸飞机时间:2026-10-30 03:12:14浏览:451
本文介绍了一种使用JavaScript实现图片高亮展示效果的方法。通过操作DOM元素和CSS样式,实现鼠标悬停时图片的放大或高亮显示,提升用户交互体验。
JavaScript实现图片高亮展示效果
本文通过一个具体的实例,详细讲解了如何利用JavaScript实现图片高亮展示的效果。该方案旨在为开发者提供实用的参考,帮助其在实际项目中应用此类交互效果。
这一功能的灵感来源于朋友的需求,旨在实现一种具有创意且易于理解的图片高亮展示效果。虽然实现过程并不复杂,但其设计思路具有一定的新颖性。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>name</title>
<style type="text/css">
*{}{margin:0; padding:0;}
a img{}{border:none;}
#imagesBox {}{ width:165px; height:110px; position:relative; left:100px; top:100px; z-index:1; border:1px solid black; background-color:white;}
#trans{}{ width:165px; height:110px; position:absolute; left:0; top:0; z-index:2; display:none; opacity:0.5; filter:alpha(opacity=50); background-color:black;}
#imagesBox li {}{ width:55px; height:55px; float:left; overflow:hidden;}
#imagesBox li a,#imagesBox li img {}{ width:55px; height:55px; display:block; cursor:pointer;}
#imagesBox li img{}{ position:relative;}
#imagesBox strong{}{position:absolute;left:0; top:50%; margin-top:-7px; z-index:999; display:block; color:white; width:100%; height:15px; text-align:center; z-index:999;}
</style>
<script type="text/javascript">
function gallery(obj){
var tit=obj.getAttribute("alt");
document.getElementById("show_tit").firstChild.nodeValue=tit;
document.getElementById("trans").style.display='block';
obj.style.zIndex="3";
}
function clearBg(obj){
document.getElementById("show_tit").firstChild.nodeValue="";
document.getElementById("trans").style.display='none';
obj.style.zIndex="1";
}
</script>
</head>
<body>
<div id="imagesBox">
<ul>
<li><a href="#" title="名称1"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名称1"/><span></span></a></li>
<li><a href="#" title="名称2"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名称2"/><span></span></a></li>
<li><a href="#" title="名称3"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名称3"/><span></span></a></li>
<li><a href="#" title="名称4"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名称4"/><span></span></a></li>
<li><a href="#" title="名称5"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名称5"/><span></span></a></li>
<li><a href="#" title="名称6"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名称6"/><span></span></a></li>
</ul>
<div id="trans"> </div>
<strong id="show_tit"> </strong>
</div>
</body>
</html>
在初步尝试阶段,由于对最终设计意图的理解存在偏差,实现的效果与预期略有不同。尽管如此,这一过程为后续优化提供了宝贵的经验。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>name</title>
<style type="text/css">
*{margin:0; padding:0;}
a img{border:none;}
#imagesBox { width:165px; height:100px; position:relative; left:100px; top:100px;}
#imagesBox li { width:55px; height:55px; float:left; overflow:hidden; position:relative;}
#imagesBox li img { width:55px; height:55px; position:relative; z-index:2; }
#imagesBox li span { line-height:100px; background:#000; position:absolute; top:0; left:0; z-index:3; opacity:0.5; filter:alpha(opacity=50);
display:block; width:55px; height:55px;}
#imagesBox li a:hover span{display:none;}
#imagesBox strong{position:absolute;left:0; top:50%; margin-top:-7px; z-index:999; display:block; color:white; width:100%; height:15px; text-
align:center;}
</style>
<script type="text/javascript">
function gallery(obj){
var tit=obj.getAttribute("title");
document.getElementById("show_tit").firstChild.nodeValue=tit;
document.getElementById("imagesBox").onmouseout=function(){document.getElementById("show_tit").firstChild.nodeValue="这里显示
名字"};
}
</script>
</head>
<body>
<div id="imagesBox">
<ul>
<li><a href="#" onmouseover="gallery(this);" title="名称1"><img src="/images/artist//12053006018793_3.jpg" height="55"
width="55" alt="名称1"/><span></span></a></li>
<li><a href="#" onmouseover="gallery(this);" title="名称2"><img src="/images/artist//12053006018793_3.jpg" height="55"
width="55" alt="名称2"/><span></span></a></li>
<li><a href="#" onmouseover="gallery(this);" title="名称3"><img src="/images/artist//12053006018793_3.jpg" height="55"
width="55" alt="名称3"/><span></span></a></li>
<li><a href="#" onmouseover="gallery(this);" title="名称4"><img src="/images/artist//12053006018793_3.jpg" height="55"
width="55" alt="名称4"/><span></span></a></li>
<li><a href="#" onmouseover="gallery(this);" title="名称5"><img src="/images/artist//12053006018793_3.jpg" height="55"
width="55" alt="名称5"/><span></span></a></li>
<li><a href="#" onmouseover="gallery(this);" title="名称6"><img src="/images/artist//12053006018793_3.jpg" height="55"
width="55" alt="名称6"/><span></span></a></li>
</ul>
<strong id="show_tit">这里显示名字</strong>
</div>
</body>
</html>
希望本文所分享的JavaScript程序设计技巧,能够为读者在相关领域的开发工作中带来启发与帮助。
作者最新文章
JS动态构建复杂嵌套JSON对象的方法
2026-11-05 15:41:52
JS负数取余为什么是负数?详解余数与模运算区别
2026-11-05 13:34:41
JS 解压出现乱码或报错怎么解决?
2026-11-05 12:32:38
JS 中 startsWith、indexOf 与正则判断字符串前缀的对比
2026-11-04 22:19:43
JS解析Excel后怎么将数据转换为JSON格式
2026-11-04 20:58:28
