我有一個頁面上的錨點,在鼠標懸停和鼠標懸停時顯示不同的背景圖像。我預先加載了圖像以避免閃爍,並在鼠標懸停/彈出時從服務器重新請求圖像。這些腳本在IE8/FF上工作正常,但Chrome的行爲有所不同。在最新版本的Chrome中,我第一次將鼠標懸停在錨點上時,服務器會重新請求圖像,導致閃爍,爲什麼?成功的鼠標懸停/退出工作正常,沒有閃爍。下面鉻圖像預加載不適用於第一個圖像加載
代碼:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<style type="text/css">
body:after
{
content: url('/images/1.png') url('/images/1a.png')
position: absolute;
top: -9999px;
left: -9999px;
}
.imageHover
{
display:inherit;
width:25px;
height:50px;
background:url('/images/1.png') no-repeat;
}
.imageOut
{
display:inherit;
width:25px;
height:50px;
background:url('/images/1a.png') no-repeat;
}
</style>
<script language="javascript" type="text/javascript">
var oneSelected = new Image();
var oneUnselected = new Image();
oneSelected.src="/images/1.png";
oneUnselected.src="/images/1a.png";
function OnImageMouseOver(target) {
$(target).toggleClass('imageHover', true);
$(target).toggleClass('imageOut', false);
}
function OnImageMouseOut(target) {
$(target).toggleClass('imageHover', false);
$(target).toggleClass('imageOut', true);
}
</script>
</head>
<body>
<a href="#" class="imageOut" onmouseover="OnImageMouseOver(this)" onmouseout="OnImageMouseOut(this)"></a>
</body>
</html>
轉換主播形象,但它仍然不會在Chrome中工作:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script language="javascript" type="text/javascript">
if (document.images) {
var oneSelected = new Image();
var oneUnselected = new Image();
oneUnselected.src = '/images/1a.png';
oneSelected.src = '/images/1.png';
}
function OnRatingMouseOver(target, newSrc) {
$(target).attr('src', newSrc);
}
function OnRatingMouseOut(target, newSrc) {
$(target).attr('src', newSrc);
}
</script>
</head>
<body>
<div id="mainDiv" style="width:400px;">
<div id="inputDiv">
<table id="inputTable">
<tr>
<td>Rating</td>
<td>
<img id='rating1Anchor'
src='/images/1a.png'
onmouseover="OnRatingMouseOver(this, '/images/1.png');"
onmouseout="OnRatingMouseOut(this, '/images/1a.png');"
onclick="OnRatingClick(this, '/images/1.png', 1);">
</td>
</tr>
</table>
</div>
</div>
</body>
<html>
這很奇怪。我只是在我的家用電腦上的Chrome上試過它,它運行得很好(圖像不需要從服務器重新請求)。任何人都可以闡明這一點嗎? – rro
要添加一些上下文,我創建的頁面是自定義頁面上的SharePoint WebPart。看起來,圖像響應標題正在以Chrome的行爲方式播放。 – rro
更多信息:在IE9和Opera中測試並且圖像不被重新請求。在Chrome中,問題似乎是間歇性的。在新打開的選項卡上,圖像被重新請求。但是在同一個窗口中打開一個新標籤,圖像不會被重新請求。單擊新選項卡上的刷新按鈕再次請求懸停和鼠標移出的圖像。 – rro