jquery實(shí)現(xiàn)的帶縮略圖的焦點(diǎn)圖片切換(自動(dòng)播放/響應(yīng)鼠標(biāo)動(dòng)作)
更新時(shí)間:2013年01月23日 15:50:05 作者:
帶縮略圖的焦點(diǎn)圖片切換在實(shí)際應(yīng)用中很廣泛的,鼠標(biāo)滑上焦點(diǎn)圖時(shí)停止自動(dòng)播放,滑出時(shí)開(kāi)始自動(dòng)播放及鼠標(biāo)滑上后顯示按鈕、顯示大圖等等,感興趣的朋友可以了解下
demo04.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>手動(dòng)滾動(dòng)圖片</title>
<style type="text/css">
ul,li{margin:0;padding:0}
img{border:0px;}
#container{padding:40px;}
#showArea img{width:700px;}
a{text-decoration:none;border:0px;}
#scrollDiv{border:#ccc 1px solid;}
#scrollDiv li{background:#A83;}
</style>
<script src="../jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="manualScroll-0.1.4.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$.manualScroll({
objId:"scrollDiv",
showArea:"showArea",
showTitle: true,
height:105,
width:140,
line:5,
speed:1000
});
});
</script>
</head>
<body>
<div id="container">
<div id="showArea"></div>
<div id="scrollDiv">
<ul>
<li><a href="#"><img src="images/1.jpg" alt="images/1.jpg" width="140"/></a></li>
<li><a href="#"><img src="images/2.jpg" alt="images/2.jpg" width="140"/></a></li>
<li><a href="#"><img src="images/3.jpg" alt="images/3.jpg" width="140"/></a></li>
<li><a href="#"><img src="images/4.jpg" alt="images/4.jpg" width="140"/></a></li>
<li><a href="#"><img src="images/5.jpg" alt="images/5.jpg" width="140"/></a></li>
<li><a href="#"><img src="images/6.jpg" alt="images/6.jpg" width="140"/></a></li>
</ul>
</div>
</div>
</body>
</html>
manualScroll-0.1.4.js
/**
* 手動(dòng)滾動(dòng)圖片
*
**/
$.extend({
manualScroll:function(opt,callback){
//alert("suc");
this.defaults = {
objId:"", // 滾動(dòng)區(qū)域id
showArea:"", // 大圖顯示區(qū)域id,如果沒(méi)有就不顯示
showWidth:700, // 大圖寬度
showHeight:525, // 大圖高度
showTitle: false, // 是否在大圖下方顯示標(biāo)題
width:300, // 每行的寬度
height:100, // div的高度
line:2, // 每次滾動(dòng)的行數(shù)
autoLine:1, // 自動(dòng)滾動(dòng)的行數(shù)
speed:0, // 動(dòng)作時(shí)間
interval:3000, // 滾動(dòng)間隔
imgPath:"", // 圖片根目錄
directBtn:"img/direct_btn02.png", // 指向圖片
picTimer:0, // 間隔句柄,不需要設(shè)置,只是作為標(biāo)識(shí)使用
opacity:0.3 // 按鈕透明度
};
//參數(shù)初始化
var opts = $.extend(this.defaults,opt);
// 定義外層元素樣式
$("#"+opts.objId).css({
"position":"relative",
"overflow":"hidden",
"width":(opts.line * opts.width) + "px"
});
// 定義ul樣式
$("#"+opts.objId + " ul").css({
"width":opts.width * $("#"+opts.objId + " ul").find("li").size() + "px",
"height":opts.height + "px"
});
// 定義li樣式
$("#"+opts.objId + " ul li").css({
"display":"block",
"float":"left",
"width":opts.width + "px",
"height":opts.height + "px"
});
// 添加向左滾動(dòng)按鈕
$("#"+opts.objId).append("<div id=\"button_left\"></div>");
// 定義向左按鈕的位置
$("#button_left").css({
"width":"40px",
"height":"40px",
"background":"url(" + opts.imgPath + opts.directBtn + ")",
"background-position":"0px 0px",
"position":"absolute",
"left":"0px",
"top":(opts.height/2 - 20) + "px"
});
// 添加向右滾動(dòng)按鈕
$("#"+opts.objId).append("<div id=\"button_right\"></div>");
// 定義向右按鈕的位置
$("#button_right").css({
"width":"40px",
"height":"40px",
"background":"url(" + opts.imgPath + opts.directBtn + ")",
"background-position":"-40px 0px",
"position":"absolute",
"left":(opts.line * opts.width - 40) + "px",
"top":(opts.height/2 - 20) + "px"
});
// 向左按鈕添加動(dòng)作
$("#button_left").click(function(){
var scrollWidth = 0 - opts.line * opts.width - (0 - $("#"+opts.objId).find("ul:first").css("margin-left").replace("px",""));
// 無(wú)間斷滾動(dòng)
$("#"+opts.objId).find("ul:first").animate({
marginLeft:scrollWidth
},opts.speed,function(){
for(i=1;i<=opts.line;i++){
$("#"+opts.objId).find("li:first").appendTo($("#"+opts.objId).find("ul:first"));
}
$("#"+opts.objId).find("ul:first").css({marginLeft:0});
showArea();
});
});
// 向右按鈕添加動(dòng)作
$("#button_right").click(function(){
var scrollWidth = (0 - opts.line*opts.width + (0 - $("#"+opts.objId).find("ul:first").css("margin-left").replace("px","")));
// 無(wú)間斷滾動(dòng)
$("#"+opts.objId).find("ul:first").animate({
marginLeft:scrollWidth
},0,function(){
for(i=1;i<=opts.line;i++){
$("#"+opts.objId).find("li:last").prependTo($("#"+opts.objId).find("ul:first"));
}
$("#"+opts.objId).find("ul:first").animate({
marginLeft:0
},opts.speed,function(){
$("#"+opts.objId).find("ul:first").css({marginLeft:0});
showArea();
});
});
});
/**
* 自動(dòng)橫向滾動(dòng)
*/
function scrollLeft(){
var scrollWidth = 0 - opts.autoLine * opts.width - (0 - $("#"+opts.objId).find("ul:first").css("margin-left").replace("px",""));
$("#"+opts.objId).find("ul:first").animate({
marginLeft:scrollWidth
},opts.speed,function(){
for(i=1;i<=opts.autoLine;i++){
$("#"+opts.objId).find("li:first").appendTo($("#"+opts.objId).find("ul:first"));
}
$("#"+opts.objId).find("ul:first").css({marginLeft:0});
showArea();
});
};
/**
* 大圖下方顯示標(biāo)題
*/
if(opts.showTitle && $("#"+opts.showArea).size() > 0){
$("#"+opts.showArea).css({
"width":opts.showWidth + "px",
"position":"relative",
"height":opts.showHeight + "px"
});
$("#"+opts.showArea).html("<img />");
$("#"+opts.showArea).append("<div id=\"manualScroll_banner\" ></div>");
$("#manualScroll_banner").css({
"width":opts.showWidth + "px",
"height":"20px",
"background":"#333",
"position":"absolute",
opacity:0.7,
"text-align":"center",
"color":"#FFF",
"left":"0px",
"top":(opts.showHeight - 20) + "px"
});
}
/**
* 在指定區(qū)域顯示大圖
*/
function showArea(){
if($("#"+opts.showArea).size() > 0){
// 顯示主圖的位置
var index = Math.floor((opts.line - 1) / 2);
showIndexArea(index);
// 鼠標(biāo)劃上后顯示大圖
$("#"+opts.objId + " ul li").each(function(index){
$(this).mouseover(function(){
showIndexArea(index);
});
});
}
}
/**
* 顯示指定元素的大圖
*/
function showIndexArea(index){
var imgSrc = $("#"+opts.objId + " ul li:eq(" + index + ") img:first").attr("src");
var imgAlt = $("#"+opts.objId + " ul li:eq(" + index + ") img:first").attr("alt");
// 淡化顯示其余圖片
$("#"+opts.objId + " ul li:lt(" + index + ")").css({
opacity:0.5
});
$("#"+opts.objId + " ul li:gt(" + index + ")").css({
opacity:0.5
});
$("#"+opts.objId + " ul li:eq(" + index + ")").css({
opacity:1
});
// 顯示大圖
$("#"+opts.showArea + " img:first").attr("src", imgSrc);
// 顯示標(biāo)題
if(opts.showTitle){
$("#manualScroll_banner").text(imgAlt);
}
}
/**
* 鼠標(biāo)滑上后顯示按鈕
*/
$("#"+opts.objId).hover(function() {
$("#button_left").css({
opacity:1
});
$("#button_right").css({
opacity:1
});
},function() {
$("#button_left").css({
opacity:opts.opacity
});
$("#button_right").css({
opacity:opts.opacity
});
}).trigger("mouseleave");
/**
* 最先執(zhí)行的函數(shù)
* 鼠標(biāo)滑上焦點(diǎn)圖時(shí)停止自動(dòng)播放,滑出時(shí)開(kāi)始自動(dòng)播放
*/
// 初始化大圖
showArea();
$("#"+opts.objId).hover(function() {
clearInterval(opts.picTimer);
},function() {
opts.picTimer = setInterval(function() {
scrollLeft();
},opts.interval); // 自動(dòng)播放的間隔,單位:毫秒
}).trigger("mouseleave");
}
});
復(fù)制代碼 代碼如下:
<!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>手動(dòng)滾動(dòng)圖片</title>
<style type="text/css">
ul,li{margin:0;padding:0}
img{border:0px;}
#container{padding:40px;}
#showArea img{width:700px;}
a{text-decoration:none;border:0px;}
#scrollDiv{border:#ccc 1px solid;}
#scrollDiv li{background:#A83;}
</style>
<script src="../jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="manualScroll-0.1.4.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$.manualScroll({
objId:"scrollDiv",
showArea:"showArea",
showTitle: true,
height:105,
width:140,
line:5,
speed:1000
});
});
</script>
</head>
<body>
<div id="container">
<div id="showArea"></div>
<div id="scrollDiv">
<ul>
<li><a href="#"><img src="images/1.jpg" alt="images/1.jpg" width="140"/></a></li>
<li><a href="#"><img src="images/2.jpg" alt="images/2.jpg" width="140"/></a></li>
<li><a href="#"><img src="images/3.jpg" alt="images/3.jpg" width="140"/></a></li>
<li><a href="#"><img src="images/4.jpg" alt="images/4.jpg" width="140"/></a></li>
<li><a href="#"><img src="images/5.jpg" alt="images/5.jpg" width="140"/></a></li>
<li><a href="#"><img src="images/6.jpg" alt="images/6.jpg" width="140"/></a></li>
</ul>
</div>
</div>
</body>
</html>
manualScroll-0.1.4.js
復(fù)制代碼 代碼如下:
/**
* 手動(dòng)滾動(dòng)圖片
*
**/
$.extend({
manualScroll:function(opt,callback){
//alert("suc");
this.defaults = {
objId:"", // 滾動(dòng)區(qū)域id
showArea:"", // 大圖顯示區(qū)域id,如果沒(méi)有就不顯示
showWidth:700, // 大圖寬度
showHeight:525, // 大圖高度
showTitle: false, // 是否在大圖下方顯示標(biāo)題
width:300, // 每行的寬度
height:100, // div的高度
line:2, // 每次滾動(dòng)的行數(shù)
autoLine:1, // 自動(dòng)滾動(dòng)的行數(shù)
speed:0, // 動(dòng)作時(shí)間
interval:3000, // 滾動(dòng)間隔
imgPath:"", // 圖片根目錄
directBtn:"img/direct_btn02.png", // 指向圖片
picTimer:0, // 間隔句柄,不需要設(shè)置,只是作為標(biāo)識(shí)使用
opacity:0.3 // 按鈕透明度
};
//參數(shù)初始化
var opts = $.extend(this.defaults,opt);
// 定義外層元素樣式
$("#"+opts.objId).css({
"position":"relative",
"overflow":"hidden",
"width":(opts.line * opts.width) + "px"
});
// 定義ul樣式
$("#"+opts.objId + " ul").css({
"width":opts.width * $("#"+opts.objId + " ul").find("li").size() + "px",
"height":opts.height + "px"
});
// 定義li樣式
$("#"+opts.objId + " ul li").css({
"display":"block",
"float":"left",
"width":opts.width + "px",
"height":opts.height + "px"
});
// 添加向左滾動(dòng)按鈕
$("#"+opts.objId).append("<div id=\"button_left\"></div>");
// 定義向左按鈕的位置
$("#button_left").css({
"width":"40px",
"height":"40px",
"background":"url(" + opts.imgPath + opts.directBtn + ")",
"background-position":"0px 0px",
"position":"absolute",
"left":"0px",
"top":(opts.height/2 - 20) + "px"
});
// 添加向右滾動(dòng)按鈕
$("#"+opts.objId).append("<div id=\"button_right\"></div>");
// 定義向右按鈕的位置
$("#button_right").css({
"width":"40px",
"height":"40px",
"background":"url(" + opts.imgPath + opts.directBtn + ")",
"background-position":"-40px 0px",
"position":"absolute",
"left":(opts.line * opts.width - 40) + "px",
"top":(opts.height/2 - 20) + "px"
});
// 向左按鈕添加動(dòng)作
$("#button_left").click(function(){
var scrollWidth = 0 - opts.line * opts.width - (0 - $("#"+opts.objId).find("ul:first").css("margin-left").replace("px",""));
// 無(wú)間斷滾動(dòng)
$("#"+opts.objId).find("ul:first").animate({
marginLeft:scrollWidth
},opts.speed,function(){
for(i=1;i<=opts.line;i++){
$("#"+opts.objId).find("li:first").appendTo($("#"+opts.objId).find("ul:first"));
}
$("#"+opts.objId).find("ul:first").css({marginLeft:0});
showArea();
});
});
// 向右按鈕添加動(dòng)作
$("#button_right").click(function(){
var scrollWidth = (0 - opts.line*opts.width + (0 - $("#"+opts.objId).find("ul:first").css("margin-left").replace("px","")));
// 無(wú)間斷滾動(dòng)
$("#"+opts.objId).find("ul:first").animate({
marginLeft:scrollWidth
},0,function(){
for(i=1;i<=opts.line;i++){
$("#"+opts.objId).find("li:last").prependTo($("#"+opts.objId).find("ul:first"));
}
$("#"+opts.objId).find("ul:first").animate({
marginLeft:0
},opts.speed,function(){
$("#"+opts.objId).find("ul:first").css({marginLeft:0});
showArea();
});
});
});
/**
* 自動(dòng)橫向滾動(dòng)
*/
function scrollLeft(){
var scrollWidth = 0 - opts.autoLine * opts.width - (0 - $("#"+opts.objId).find("ul:first").css("margin-left").replace("px",""));
$("#"+opts.objId).find("ul:first").animate({
marginLeft:scrollWidth
},opts.speed,function(){
for(i=1;i<=opts.autoLine;i++){
$("#"+opts.objId).find("li:first").appendTo($("#"+opts.objId).find("ul:first"));
}
$("#"+opts.objId).find("ul:first").css({marginLeft:0});
showArea();
});
};
/**
* 大圖下方顯示標(biāo)題
*/
if(opts.showTitle && $("#"+opts.showArea).size() > 0){
$("#"+opts.showArea).css({
"width":opts.showWidth + "px",
"position":"relative",
"height":opts.showHeight + "px"
});
$("#"+opts.showArea).html("<img />");
$("#"+opts.showArea).append("<div id=\"manualScroll_banner\" ></div>");
$("#manualScroll_banner").css({
"width":opts.showWidth + "px",
"height":"20px",
"background":"#333",
"position":"absolute",
opacity:0.7,
"text-align":"center",
"color":"#FFF",
"left":"0px",
"top":(opts.showHeight - 20) + "px"
});
}
/**
* 在指定區(qū)域顯示大圖
*/
function showArea(){
if($("#"+opts.showArea).size() > 0){
// 顯示主圖的位置
var index = Math.floor((opts.line - 1) / 2);
showIndexArea(index);
// 鼠標(biāo)劃上后顯示大圖
$("#"+opts.objId + " ul li").each(function(index){
$(this).mouseover(function(){
showIndexArea(index);
});
});
}
}
/**
* 顯示指定元素的大圖
*/
function showIndexArea(index){
var imgSrc = $("#"+opts.objId + " ul li:eq(" + index + ") img:first").attr("src");
var imgAlt = $("#"+opts.objId + " ul li:eq(" + index + ") img:first").attr("alt");
// 淡化顯示其余圖片
$("#"+opts.objId + " ul li:lt(" + index + ")").css({
opacity:0.5
});
$("#"+opts.objId + " ul li:gt(" + index + ")").css({
opacity:0.5
});
$("#"+opts.objId + " ul li:eq(" + index + ")").css({
opacity:1
});
// 顯示大圖
$("#"+opts.showArea + " img:first").attr("src", imgSrc);
// 顯示標(biāo)題
if(opts.showTitle){
$("#manualScroll_banner").text(imgAlt);
}
}
/**
* 鼠標(biāo)滑上后顯示按鈕
*/
$("#"+opts.objId).hover(function() {
$("#button_left").css({
opacity:1
});
$("#button_right").css({
opacity:1
});
},function() {
$("#button_left").css({
opacity:opts.opacity
});
$("#button_right").css({
opacity:opts.opacity
});
}).trigger("mouseleave");
/**
* 最先執(zhí)行的函數(shù)
* 鼠標(biāo)滑上焦點(diǎn)圖時(shí)停止自動(dòng)播放,滑出時(shí)開(kāi)始自動(dòng)播放
*/
// 初始化大圖
showArea();
$("#"+opts.objId).hover(function() {
clearInterval(opts.picTimer);
},function() {
opts.picTimer = setInterval(function() {
scrollLeft();
},opts.interval); // 自動(dòng)播放的間隔,單位:毫秒
}).trigger("mouseleave");
}
});
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)級(jí)聯(lián)菜單效果(仿淘寶首頁(yè)菜單動(dòng)畫)
- jQuery實(shí)現(xiàn)的鼠標(biāo)響應(yīng)緩沖動(dòng)畫效果示例
- jQuery實(shí)現(xiàn)的下雪動(dòng)畫效果示例【附源碼下載】
- jquery animate動(dòng)畫持續(xù)運(yùn)動(dòng)的實(shí)例
- jquery animate 動(dòng)畫效果使用說(shuō)明
- 分享8款優(yōu)秀的 jQuery 加載動(dòng)畫和進(jìn)度條插件
- jQuery Animation實(shí)現(xiàn)CSS3動(dòng)畫示例介紹
- jQuery實(shí)現(xiàn)加入購(gòu)物車飛入動(dòng)畫效果
- 用js實(shí)現(xiàn)的模擬jquery的animate自定義動(dòng)畫(2.5K)
- jQuery插件Slider Revolution實(shí)現(xiàn)響應(yīng)動(dòng)畫滑動(dòng)圖片切換效果
- jquery多行滾動(dòng)/向左或向上滾動(dòng)/響應(yīng)鼠標(biāo)實(shí)現(xiàn)思路及代碼
- jQuery實(shí)現(xiàn)鼠標(biāo)響應(yīng)式淘寶動(dòng)畫效果示例
相關(guān)文章
jQuery實(shí)現(xiàn)每隔幾條元素增加1條線的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)每隔幾條元素增加1條線的方法,可實(shí)現(xiàn)每隔10條li元素增加一條虛線的功能,涉及jQuery元素的匹配與屬性動(dòng)態(tài)設(shè)置技巧,需要的朋友可以參考下2016-06-06用jQuery toggleClass 實(shí)現(xiàn)鼠標(biāo)移上變色
這篇文章主要介紹了用jQuery toggleClass 實(shí)現(xiàn)鼠標(biāo)移上變色,需要的朋友可以參考下2014-05-05QRCode.js:基于JQuery的生成二維碼JS庫(kù)的使用
本篇文章主要介紹了QRCode.js:基于JQuery的生成二維碼JS庫(kù)的使用,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下2017-06-06JQuery 選擇器 xpath 語(yǔ)法應(yīng)用
我們根據(jù)實(shí)例來(lái)解釋JQuery選擇器(selectors)中xpath幾種常用的用法2010-05-05jquery實(shí)現(xiàn)手風(fēng)琴效果
這篇文章主要介紹了jquery實(shí)現(xiàn)手風(fēng)琴效果,像手風(fēng)琴一樣打開(kāi),立體感效果比較強(qiáng),感興趣的小伙伴們可以參考一下2015-11-11