「滑過就跳出大圖」在圖多列表頁很容易變成騷擾,加上一段延遲判斷就能大幅改善體驗:只有停留超過一段時間才視為真的想看,才把大圖載入出來。
<style>
*{margin:0;padding:0;list-style-type:none;}
img,a{border:0;}
.piccon{height:75px;margin:100px 0 0 50px;}
.piccon li{float:left;padding:0 10px;}
#preview{position:absolute;border:1px solid #ccc;background:#333;padding:5px;display:none;color:#fff;z-index:2000;}
</style>
<script type="text/javascript">
this.imagePreview = function(){
xOffset = 10;
yOffset = 30;
var urll;
$(".widget .ks-content a,.box .ks-switchable-content div li a").hover(function(){
var goods_id = $(this).attr("href").replace("index.php?app=goods&id=", "");
this.t = this.title;
this.title = " ";
var c = (this.t != "") ? "<br/>" + this.t : " ";
$.post("index.php?app=default&act=ajaxBigImage", {goods_id: goods_id}, function(data){
$.ajaxSettings.async = false;
if(data != 0){
urll = data;
$("body").append("<div id='preview'><img src=" + urll + " />" + c + "</div>");
return true;
}else{
return false;
}
});
$('#preview').css({
position: 'absolute',
left: ($(window).width() - $('#preview').outerWidth()) / 2,
top: ($(window).height() - $('#preview').outerHeight()) / 2 + $(document).scrollTop()
});
setTimeout(function(){
$("<#preview>").fadeIn("fast");
}, 3000)
},
function(){
this.title = this.t;
$("#preview").remove();
});
};
$(document).ready(function(){
imagePreview();
});
</script>
<div id="_widget_579" name="jdr_sale_list" widget_type="widget" class="widget">
<div class="sale_list mt10 clearfix">
<h2>銷售排行</h2>
<div>
<ul class="ks-nav ks-nav1476271702">
<li class="ks-active nav1">特效禮花</li>
<li class="nav2">套餐煙花</li>
</ul>
<div class="ks-content">
<ul class="box" id="box1_1476271702">
<li>
<div class="pic"><a href="index.php?app=goods&id=331"><img alt="【81發六錦系列組合煙花】" src="data/files/store_927/goods_51/small_201312121557314803.JPG" height="60" width="60"></a><b>1</b></div>
<div class="info">
<div class="title"><a href="index.php?app=goods&id=331">【81發六錦系列組合煙花】</a></div>
<div class="price">¥200.00</div>
</div>
</li>
<!-- 其餘 li 結構相同 -->
</ul>
<ul class="box box2" id="box2_1476271702" style="display:none;">
<!-- 第二組商品 -->
</ul>
</div>
</div>
</div>
</div>
//ajax
function ajaxBigImage()
{
if(!empty($_POST['goods_id'])){
$goods_id = $_POST['goods_id'];
$goodsimg_mod = & m('goodsimage');
$uploadedfile_mod = & m('uploadedfile');
// 取得 file_id 字串
$file_arr = $goodsimg_mod->getCol("select file_id from `ecm_goods_image` WHERE goods_id={$goods_id} ORDER BY file_id DESC");
if(!empty($file_arr)){
$file_str = implode(",", $file_arr);
}
$url_arr = $uploadedfile_mod->getAll("select file_type,file_path from `ecm_uploaded_file` WHERE file_id IN({$file_str}) ORDER BY add_time DESC");
if(!empty($url_arr)){
foreach ($url_arr as $v){
if($v['file_type'] == 'image/gif'){
$url = $v['file_path'];
break;
}else {
unset($v);
}
}
}
if(!empty($url)){
echo $url;
exit();
}else{
echo 0;
exit();
}
}else{
echo 0;
}
}
原範例存在幾處值得修正的地方,實作時請留意:
延遲沒有被取消。這是最關鍵的缺陷。目前的寫法在 hover 時啟動 setTimeout,但滑鼠移出時並沒有 clearTimeout,所以使用者一閃而過仍然會在 3 秒後跳出大圖。正確做法是把計時器 id 存起來:
var timer = null;
$("#list a").hover(function(){
timer = setTimeout(function(){ /* 顯示大圖 */ }, 3000);
}, function(){
clearTimeout(timer); // 提前離開就取消
$("#preview").remove();
});
3 秒偏長。實務上 300~500 毫秒就足以過濾掉「路過」的滑鼠,3 秒會讓自覺有意圖的使用者覺得卡頓。
$.ajaxSettings.async = false; 是危險寫法。它會全域改掉 jQuery 的 AJAX 設定,影響同一頁面上所有後續請求,而且同步請求會凍結整個 UI。應該改成在該次請求的選項裡指定,或更好的作法是先顯示 loading、等回應回來再填入內容。
HTML 拼接有 XSS 風險。urll 直接被串進 img src,若後端回傳的內容可被使用者影響就有問題。改用 jQuery 建立元素並以 .attr('src', urll) 設定會安全得多。後端 SQL 也應改用參數化查詢,避免 $goods_id 直接內插字串造成 SQL Injection。
先forthcoming img 預載。大圖在 hover 之後才開始下載,會有明顯的空白等待。可以在頁面閒置時預先載入常用尺寸,或用 new Image().src = url 提前溫熱快取。
另外範例裡的 $("#preview") 在 append 之前就先取用,此時它還不存在,outerWidth() 會回傳 0,置中計算其實是錯的。合理順序是「先加入 DOM → 再計算位置 → 最後淡入」。