[英]javascript/css: Make image expand up and to left on rollover effect
我想让用户扩展缩略图,以便在鼠标悬停时查看大型个人资料照片。 但是,我当前的翻转效果将表格行扩展为展开图像的大小,而不是在周围文本上显示图像。 任何人都可以建议一种方法来扩展图像翻转而不移动文本,换句话说,在其他HTML上显示更大的图像,而不是推迟它。 我尝试过z-index,这似乎不起作用。
这是一个演示问题的jsfiddle:
这里的代码与js小提琴中的代码相同:
<script language="JavaScript">
<!-- hide from non JavaScript Browsers
image = new Array()
image[0]= new Image(24,24)
image[0].src = "http://womeninbiznetwork.com/wp-content/uploads/2011/07/logo-google.thumbnail.gif"
image[1] = new Image(48,48)
image[1].src = "https://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif"
</script>
<tr><td colspan=2>
<p align="center">
<a href="contactdetail.php"
onmouseover="newPic()"
onmouseout="oldPic()">
<img src="http://womeninbiznetwork.com/wp-content/uploads/2011/07/logo-google.thumbnail.gif"
name="pic"
class="rollover"
border=0>
</a>
</p></td></tr>
<tr><td>Here is text</td></tr>
JS
function newPic(){
document.pic.style.left="-24px";
document.pic.style.top="-24px";
document.pic.src = image[1].src;
return true;
}
function oldPic(){
document.pic.src = image[0].src;
return true;
}
CSS:
#rollover {
display: block;
z-index:99;
padding: 4px;
position:relative;
text-align:left;
width:48px;
background-color:#E8E8E8;
opacity:1.0;
filter:alpha(opacity=100);
}
从CSSplay中窃取 - 放大 ,你可以在没有Javascript的情况下做到这一点
HTML:
<tr>
<td colspan=2>
<p align="center"> <a class="rollover" href="contactdetail.php">
<img src="http://womeninbiznetwork.com/wp-content/uploads/2011/07/logo-google.thumbnail.gif" name="pic" border="0"/>
<span><img src="http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif"/></span>
</a>
</p>
</td>
</tr>
<tr>
<td>Here is text</td>
</tr>
CSS:
a.rollover {
display: block;
position: relative;
}
a.rollover span {
position: absolute;
left: -10000px;
}
a.rollover:hover span {
position: absolute;
left: 200px;
}
更新 :
我不知道,如果这是您的想法,但您可以将更大的图像放在任何您想要的位置
CSS:
a.rollover:hover span {
position: absolute;
left: 120px;
top: -20px;
}
如果要在悬停时隐藏下部图像,可以尝试添加opacity: 0
<img class="thumbnail" src="...">
和
a.rollover:hover .thumbnail {
opacity: 0;
}
虽然,我不知道这是否适用于每个浏览器。
一种方法是克隆你想要的所有<img>
节点,将它们放在<div>
,然后使用CSS :hover
为你做剩下的工作。 例子小提琴 。
我的代码可以在文档中的任何位置使用任意数量的<img>
,并且不需要固定大小的父节点。
如果由于JavaScript对HTML结构的更改而导致其他样式不符合您的预期,您可能需要修改我所选择的CSS或类名(请参阅下面的“生成的HTML”)。
// JavaScript
window.onload = function () { // when document has loaded, consider `window.addEventListener('load', /* code */, false);` in real world usage
var elm = document.querySelectorAll('img.rollover'), // get all <img>s with class "rollover"
i = elm.length, d;
while (--i >= 0) { // put each one and a clone into a <div> where it was
d = elm[i].parentNode.insertBefore(document.createElement('div'), elm[i]);
d.className = 'rollover';
elm[i].className = (' '+elm[i].className+' ') // set class'
.replace(/ rollover /g, ' ')
.slice(1, -1);
d.appendChild(elm[i]);
d.appendChild(elm[i].cloneNode()).className += ' rollover_big'; // I did clone here to save needing an extra variable
elm[i].className += ' rollover_small';
}
};
/* CSS */
div.rollover {position: relative; display: block;} /* relative to allow for abs. positioning of big image */
div.rollover img.rollover_small {width: 24px;}
div.rollover img.rollover_big { /* position over small image*/
width: 48px;
display: none;
position: absolute;
top: 0px;
}
div.rollover:hover img.rollover_small {visibility: hidden;} /* visibility to hold space*/
div.rollover:hover img.rollover_big {display: block;} /* show bigger node*/
<!-- HTML -->
<table>
<tr>
<td colspan=2><a href="contactdetail.php"><img src="http://womeninbiznetwork.com/wp-content/uploads/2011/07/logo-google.thumbnail.gif" class="rollover"/></a></td>
</tr>
<tr>
<td>Here is text</td>
</tr>
<!-- etc. -->
</table>
<!-- Resulting HTML after load event -->
<table>
<tr>
<td colspan=2><a href="contactdetail.php">
<div class="rollover">
<img src="http://womeninbiznetwork.com/wp-content/uploads/2011/07/logo-google.thumbnail.gif" class="rollover_small"/>
<img src="http://womeninbiznetwork.com/wp-content/uploads/2011/07/logo-google.thumbnail.gif" class="rollover_big"/>
</div>
</a></td>
</tr>
<tr>
<td>Here is text</td>
</tr>
<!-- etc. -->
</table>
你不需要javascript。 请参阅jsFiddle上的工作演示 。
用CSS做 :
td.imgCell {
height: 40px; /* a value greater than image height + paddings */
text-align: center;
}
img.rollover {
display: inline-block;
position: relative;
padding: 4px;
width: 24px;
height: 24px;
background-color:#E8E8E8;
border: 0px none;
}
img.rollover:hover {
position: absolute;
width: 48px;
height: 48px;
margin-top: -24px;
margin-left: -24px;
z-index: 99;
}
这是修改后的HTML :
<table>
<tr>
<td class="imgCell">
<a href="contactdetail.php">
<img class="rollover" src="http://womeninbiznetwork.com/wp-content/uploads/2011/07/logo-google.thumbnail.gif" />
</a>
</td>
</tr>
<tr>
<td>Here is text</td>
</tr>
</table>
请注意,还声明并设置了容器td的类: <td class="imgCell">
关键点:
<td>
应该具有固定的高度。 absolute
位置。 其他更正/建议:
colspan
属性。 colspan属性定义单元格应跨越的列数。 您不必这样做,因为每行( <tr>
)中只有一个<td>
。 <p>
标记。 但这没错。 如果你坚持使用那个JS代码;
</script>
标记之前包含//-->
因为您在开始时<!-- hide from non JavaScript Browsers
。 var
声明image变量,否则它将在全局范围内。 []
而不是new Array()
来初始化数组,就像jsLint建议的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.