jquery焦点图片切换(数字标注/手动/自动播放/横向滚动)

 更新时间:2013年01月24日 11:28:25   作者:  
焦点图片切换在网页制作的商品展示中经常会用到,这样的效果可以给用户带来耳目一新的感觉同时也是用户比较追捧的,本文也实现了一个这样的焦点图片切换效果,感兴趣的你可以参考下啊,希望本文对你有所帮助

demo01.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>手动滚动图片</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="imgfocus-0.1.0.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){

$.imgfocus({
objId:"scrollDiv",
showTitle: true,
height:210,
width:280,
speed:1000
});
});
</script>
</head>
<body>
<div id="container">
<div id="scrollDiv">
<ul>
<li><a href="#"><img src="images/1.jpg" alt="images/1.jpg" width="280"/></a></li>
<li><a href="#"><img src="images/2.jpg" alt="images/2.jpg" width="280"/></a></li>
<li><a href="#"><img src="images/3.jpg" alt="images/3.jpg" width="280"/></a></li>
<li><a href="#"><img src="images/4.jpg" alt="images/4.jpg" width="280"/></a></li>
<li><a href="#"><img src="images/5.jpg" alt="images/5.jpg" width="280"/></a></li>
<li><a href="#"><img src="images/6.jpg" alt="images/6.jpg" width="280"/></a></li>
</ul>
</div>
</div>
</body>
</html>

imgfocus-0.1.0.js
复制代码 代码如下:

/**
* 手动滚动图片
*
**/
$.extend({
imgfocus: function(opt, callback) {
//alert("suc");
this.defaults = {
// 滚动区域id
objId: "",
// 是否在大图下方显示标题
showTitle: false,
// 每行的宽度
width: 300,
// div的高度
height: 100,
// 每次滚动的行数
line: 1,
// 自动滚动的行数
autoLine: 1,
// 动作时间
speed: 0,
// 滚动间隔
interval: 3000,
// 图片根目录
imgPath: "",
// 间隔句柄,不需要设置,只是作为标识使用
picTimer: 0,
// 按钮透明度
opacity: 0.3
};
//参数初始化
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"
});
// 定义img样式
$("#" + opts.objId + " ul li img:first").css({
"display": "block",
"float": "left",
"width": opts.width + "px",
"height": opts.height + "px"
});
if (opts.showTitle) {
$("#" + opts.objId).append("<div id=\"imgfocus_banner\" />");
$("#imgfocus_banner").css({
"width": opts.width + "px",
"height": "20px",
"background": "#333",
"position": "absolute",
opacity: 0.7,
"text-align": "center",
"color": "#FFF",
"left": "0px",
"top": (opts.height - 20) + "px"
});
$("#imgfocus_banner").html("<div id=\"imgfocus_banner_title\" />");
$("#imgfocus_banner_title").text("text");
$("#imgfocus_banner_title").css({
"display": "block",
"float": "left",
"width": (opts.width - 20 * $("#" + opts.objId + " ul li").size()) + "px",
"height": "20px"
});
$("#" + opts.objId + " ul li").each(function(index) {
$(this).attr("index", index);
$("#imgfocus_banner").append("<div id=\"imgfocus_banner_squ" + index + "\" class=\"imgfocus_banner_squ\" >" + (index + 1) + "</div>");
var bgColor;
$("#imgfocus_banner_squ" + index).mouseover(function() {
bgColor = $(this).css("background");
$(this).css({
"background": "#CC0"
});
}).mouseleave(function() {
$(this).css({
"background": bgColor
});
});
// 数字块点击事件
$("#imgfocus_banner_squ" + index).click(function() {

var length = $("#" + opts.objId + " ul li[index=" + index + "]").prevAll().size();
var scrollWidth = 0 - length * opts.width - (0 - $("#" + opts.objId).find("ul:first").css("margin-left").replace("px", ""));
$("#" + opts.objId).find("ul:first").animate({
marginLeft: scrollWidth
},
6,
function() {
for (i = 1; i <= length; i++) {
$("#" + opts.objId).find("li:first").appendTo($("#" + opts.objId).find("ul:first"));
}
$("#" + opts.objId).find("ul:first").css({
marginLeft: 0
});
var index = $("#" + opts.objId).find("li:first").attr("index");
// 数字标签全部变灰色
$(".imgfocus_banner_squ").css({
"background": "#CCC"
});
// 活动的数字标签变红色
$("#imgfocus_banner_squ" + index).css({
"background": "#C00"
});
bgColor = "background:#C00";
changeTitle();
});
});
});
// 数字块样式
$(".imgfocus_banner_squ").css({
"display": "block",
"float": "left",
"margin": "1px",
"width": "18px",
"height": "18px",
"color": "#000",
"background": "#CCC"
});
// 第一个数字块样式
$(".imgfocus_banner_squ:first").css({
"background": "#C00"
});
}
/**
* 自动横向滚动
*/
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
});
var index = $("#" + opts.objId).find("li:first").attr("index");
changeTitle();
// 数字标签全部变灰色
$(".imgfocus_banner_squ").css({
"background": "#CCC"
});
// 活动的数字标签变红色
$("#imgfocus_banner_squ" + index).css({
"background": "#C00"
});
});
};
/**
* 切换标题
*/
function changeTitle(){
$("#imgfocus_banner_title").text($("#" + opts.objId).find("li:first img:first").attr("alt"));
}
/**
* 鼠标滑上后显示按钮
*/
$("#" + 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");
/**
* 最先执行的函数
* 鼠标滑上焦点图时停止自动播放,滑出时开始自动播放
*/
// 初始化标题
changeTitle();
$("#" + opts.objId).hover(function() {
clearInterval(opts.picTimer);
},
function() {
opts.picTimer = setInterval(function() {
scrollLeft();
},
opts.interval); // 自动播放的间隔,单位:毫秒
}).trigger("mouseleave");
}
});

相关文章

  • jQuery LigerUI 使用教程入门篇

    jQuery LigerUI 使用教程入门篇

    jQuery LigerUI 是基于jQuery的一系列UI控件组合,简单而又强大,致力于快速打造Web前端界面解决方案。 因为是前端控件,跟服务器无关,可以适合.net,jsp,php等等web服务器环境
    2012-01-01
  • jquery实现的V字形显示效果代码

    jquery实现的V字形显示效果代码

    这篇文章主要介绍了jquery实现的V字形显示效果代码,涉及jQuery数学运算及样式操作的相关技巧,需要的朋友可以参考下
    2015-10-10
  • jQuery实现简洁的导航菜单效果

    jQuery实现简洁的导航菜单效果

    这篇文章主要介绍了jQuery实现简洁的导航菜单效果,简洁大方的导航菜单,需要的朋友可以参考下
    2015-11-11
  • jQuery实现图片与文字描述左右滑动自动切换的方法

    jQuery实现图片与文字描述左右滑动自动切换的方法

    这篇文章主要介绍了jQuery实现图片与文字描述左右滑动自动切换的方法,涉及jquery实现图文滑动切换效果的方法,涉及jquery针对页面元素与样式的相关操作技巧,需要的朋友可以参考下
    2015-07-07
  • Jquery揭秘系列:ajax原生js实现详解(推荐)

    Jquery揭秘系列:ajax原生js实现详解(推荐)

    下面小编就为大家带来一篇Jquery揭秘系列:ajax原生js实现详解(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • 使用jQuery实现WordPress中的Ctrl+Enter和@评论回复

    使用jQuery实现WordPress中的Ctrl+Enter和@评论回复

    相信大家对QQ中的Ctrl+Enter快捷回复和微博上的@指名回复功能都不陌生,在WordPress的评论栏方面我们同样可以添加这样的功能,一起来看使用jQuery实现WordPress中的Ctrl+Enter和@评论回复的方法:
    2016-05-05
  • jQuery Div中加载其他页面的实现代码

    jQuery Div中加载其他页面的实现代码

    在做一个表单签核系统时,需在要签核页面中将表单内容(事先做好的PHP页面)显示出来,于就是想能不能利用Ajax技术把这个事先做好的页面嵌入到签核页面中呢?
    2009-02-02
  • 推荐10 个很棒的 jQuery 特效代码

    推荐10 个很棒的 jQuery 特效代码

    本文给大家汇总介绍了10个非常实用,也非常简单的jquery特效,基本上项目中都可以用到,推荐给大家,有需要的小伙伴可以参考下。
    2015-10-10
  • jquery 为a标签绑定click事件示例代码

    jquery 为a标签绑定click事件示例代码

    jquery 为a标签绑定click事件,当被点击时执行一些动作,示例代码如下,需要的朋友可以参考参考
    2014-06-06
  • jquery maxlength使用说明

    jquery maxlength使用说明

    在本教程中,我将告诉你如何使用一些常见的形式领域jquery.maxlength.js
    2011-09-09

最新评论