【发布时间】:2011-12-09 18:33:45
【问题描述】:
我有一个物品清单。它们每个都是一个有名字的正方形,当用户将鼠标悬停在正方形上时,会显示一个图像,这是由 jQuery 完成的。代码是这样的:
$('.square').hover(function() {
var link = $(this).attr('title');
$(this).addClass('squarehover')
.css({backgroundImage: 'url(images/' + escape(link) + '.jpg)'});
}, function() {
$(this).removeClass('squarehover')
.attr('style', '');
});
.squarehover {
font-size: 0px;
background-repeat: no-repeat;
background-size: 100%;
-moz-background-size: 100%;
background-position: center center;
position: absolute;
z-index: 10;
cursor: pointer;
}
如果没有预先加载图片,悬停时会有一点延迟。另外,我不想在加载完所有图像后显示页面。
由于页面中没有明确显示图像,是否可以先加载页面然后开始将图像加载到用户的缓存中(我的想法是用户不会立即将鼠标移动到那些方块上)?如果 HTML 中没有 <img> 标签怎么办?
顺便说一句,由于IE8不支持background-size,如何处理?
【问题讨论】:
标签: javascript html image