完美兼容各大浏览器的jQuery插件实现图片切换特效

文件里面的功能注释也写得非常详细(详见zoeDylan.ImgChange-1.0.1.js文件),对网友们的学习是很有帮助的,虽然样式不太好看,大家可以自己写,好好利用哦。。。

JS代码部分:

代码如下:

(function ($) {
    var//申明全局变量
        _eleTemp,//缓存变量
        _eleThis = $(this),//当前元素
        _eleImg = $('.zd-imgChange-img'),//图片组元素
        _eleControll = $('.zd-imgChange-controll'),//控制器组元素
        _eleChange = $('.zd-imgChange-change'),//切换元素
            _icon = '●○',//动态图标
            _imgTemplate = $(document.createElement('img')),//图片模版'
            _setting = {
                height: 100,//高
                width: 200,//宽
                imgs: new Array(),//图片地址
                links: null,//点击地址
                tips: null,//图片说明
                timers: 3000//自动切换时间
            },//配置
            _timers = null//自动切换保存变量
    $.fn.zoeDylan_ImageChange = function (op) {
        _eleThis = $(this);
        _setting = $.extend(_setting, op);//设置属性
        //处理数据(查看是否为合法范围)
        _setting.timers = _setting.timers != null && _setting.timers != undefined && _setting.timers > 1000 ? _setting.timers : 1000;
        _setting.height = parseFloat(_setting.height) < 100 ? 100 : parseFloat(_setting.height);
        _setting.width = parseFloat(_setting.width) < 200 ? 200 : parseFloat(_setting.width);
        return this.each(function () {//初始化
            AddTemplate();
            AddImg();
            DisSize();
            DisControll();
        })
    }
    //添加模版
    function AddTemplate() {
        _eleThis.removeClass();
        _eleThis.addClass('zd-imgChange');
        _eleTemp = '<button class="zd-imgChange-change zd-imgChange-change-left"><</button>';
        _eleThis.append(_eleTemp);
        _eleTemp = '     <div class="zd-imgChange-img">   </div>';
        _eleThis.append(_eleTemp);
        _eleTemp = '       <div class="zd-imgChange-controll"></div>';
        _eleThis.append(_eleTemp);
        _eleTemp = ' <button class="zd-imgChange-change zd-imgChange-change-right">></button>';
        _eleThis.append(_eleTemp);
        //给全局变量赋值
        _eleImg = $('.zd-imgChange-img');
        _eleControll = $('.zd-imgChange-controll');
        _eleChange = $('.zd-imgChange-change');
    }
    //添加图片
    function AddImg() {
        for (var i = 0; i < _setting.imgs.length; i++) {
            //图片
            _eleTemp = $(document.createElement('img'));
            _eleTemp.addClass('zd-imgChange-img-item');
            _eleTemp.attr('src', _setting.imgs[i]);
            _eleImg.append(_eleTemp);
            //图标
            _eleControll.append('<span>' + _icon[1] + '</span>');
            //提示
            $(_eleImg.children('img')[i]).attr({ 'title': _setting.tips[i] + '|' + _setting.links[i] });
            $(_eleControll.children('span')[i]).attr('title', _setting.tips[i]);
        }
        Dispose(0, 'TtoB');
    }
    //处理尺寸
    function DisSize() {
        _eleThis.css({ 'height': _setting.height, 'width': _setting.width, 'font-size': _setting.height * 0.2 - 2 });
        _eleThis.children('button').css('font-size', _setting.height * 0.2 - 2);
        _eleChange.css({ 'line-height': _setting.height + 'px' });
    }
    //处理操作事件
    function DisControll() {
        _eleImg.children('img').bind('click', function () {//点击图片跳转
            document.location = 'http://' + $(this).attr('title').split('|')[$(this).attr('title').split('|').length - 1];
        });
        _eleChange.bind('click', function () {//顺序切换图片
            var nowImg = $('.zd-imgChange-img-item-sel');
            _eleTemp = _eleImg.children('img');
            if ($(this).hasClass('zd-imgChange-change-left')) {//向左切换
                for (var i = 0; i < _eleTemp.length; i++) {
                    if (nowImg.attr('title') == $(_eleTemp[i]).attr('title')) {
                        if ((i - 1) < 0) {
                            Dispose(_eleTemp.length - 1, 'RtoL');
                        } else {
                            Dispose(i - 1, 'RtoL');
                        }
                        break;
                    }
                }
            } else if ($(this).hasClass('zd-imgChange-change-right')) {//向右切换
                for (var i = 0; i < _eleTemp.length; i++) {
                    if (nowImg.attr('title') == $(_eleTemp[i]).attr('title')) {
                        if ((i + 1) > _eleTemp.length - 1) {
                            Dispose(0, 'LtoR');
                        } else {
                            Dispose(i + 1, 'LtoR');
                        }
                        break;
                    }
                }
            } else {
                return false;
            }
        });
        _eleControll.children('span').bind('click', function () {
            for (var i = 0; i < _eleControll.children('span').length; i++) {
                if ($(_eleControll.children('span')[i]).attr('title') == $(this).attr('title')) {
                    if ($(_eleImg.children('img')[i]).attr('src') != $('.zd-imgChange-img-item-sel').attr('src')) {//判断是否点击的同一个标签
                        Dispose(i, 'TtoB');
                    }
                    break;
                }
            }
        });
    }
    //切换器
    function Dispose(eNum, dir) {//切换图片 
        clearTimeout(_timers);
        DisposeAnm();
        $(_eleControll.children('span')).html(_icon[1]);
        $(_eleControll.children('span')[eNum]).html(_icon[0]);
        $('.zd-imgChange-change-left').attr('title', eNum - 1 >= 0 ? $(_eleControll.children('span')[eNum - 1]).attr('title') : $(_eleControll.children('span')[_eleControll.children('span').length - 1]).attr('title'));
        $('.zd-imgChange-change-right').attr('title', eNum + 1 <= _eleControll.children('span').length - 1 ? $(_eleControll.children('span')[eNum + 1]).attr('title') : $(_eleControll.children('span')[0]).attr('title'));
        _timers = setTimeout(Timers, _setting.timers);
        //切换动画--根据z-index实现视觉效果
        function DisposeAnm() {
            $('.zd-imgChange-img-item').removeClass('zd-imgChange-img-item-temp');
            $('.zd-imgChange-img-item-sel').removeClass('zd-imgChange-img-item-sel').addClass('zd-imgChange-img-item-temp');
            _eleTemp = $(_eleImg.children('img')[eNum]).addClass('zd-imgChange-img-item-sel');
            if (dir == 'TtoB') {
                _eleTemp.css('top', -_setting.height);
                _eleTemp.animate({ top: 0 }, 300);
            } else if (dir == 'RtoL') {
                _eleTemp.css('left', _setting.width);
                _eleTemp.animate({ left: 0 }, 300);
            } else if (dir == 'LtoR') {
                _eleTemp.css('left', -_setting.width);
                _eleTemp.animate({ left: 0 }, 300);
            }
        }
    }
    //自动切换时钟
    function Timers() {
        _timers = setTimeout(Timers, _setting.timers);
        var nowImg = $('.zd-imgChange-img-item-sel');
        _eleTemp = _eleImg.children('img');
        for (var i = 0; i < _eleTemp.length; i++) {
            if (nowImg.attr('title') == $(_eleTemp[i]).attr('title')) {
                if ((i - 1) < 0) {
                    Dispose(_eleTemp.length - 1, 'TtoB');
                } else {
                    Dispose(i - 1, 'TtoB');
                }
                break;
            }
        }
    }
})($)

CSS代码部分:

代码如下:

.zd-imgChange {
    position: relative;
    margin: auto;
    padding: 0px;
    min-width: 200px;
    min-height: 100px;
    background: rgba(0,0,0,0.5);
    color: #fff;
    cursor: pointer;
    overflow: hidden;
}
.zd-imgChange-change {
    cursor:pointer;
    color:#fff;
    margin: 0px;
    padding: 0px;
    position: relative;
    background: rgba(0,0,0,0.5);
    width: 10%;
    height: 100%;
    text-align: center;
    opacity: 0.1;
    z-index: 1;
    -moz-transition: opacity 0.4s;
    -o-transition: opacity 0.4s;
    -webkit-transition: opacity 0.4s;
    transition: opacity 0.4s;
    border:none;
}
    .zd-imgChange-change:hover {
        opacity: 1;
    }
.zd-imgChange-change-left {
    float: left;
}
.zd-imgChange-change-right {
    float: right;
}
.zd-imgChange-img {
    z-index: 0;
    padding: 0px;
    position: absolute;
    top: 0px;
    left: 0px;
    background: rgba(100,0,0,0.1);
    width: 100%;
    height: 100%;
}
.zd-imgChange-img-item {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    border: none;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    -moz-transition: opacity 0.5s;
    -o-transition: opacity 0.5s;
    -webkit-transition: opacity 0.5s;
    transition: opacity 0.5s;
    opacity: 1;
    z-index: -1;
}
.zd-imgChange-img-item-temp{
    z-index:0;
}
.zd-imgChange-img-item-sel {
    z-index: 1;
}
.zd-imgChange-controll {
    z-index: 2;
    padding: 0px;
    position: absolute;
    bottom: 0px;
    width: 100%;
    height: 20%;
    background: rgba(0,0,0,0);
    text-align: center;
    -moz-transition: background 0.4s;
    -o-transition: background 0.4s;
    -webkit-transition: background 0.4s;
    transition: background 0.4s;
    text-shadow: 0px 0px 5px #000;
    opacity: 0.7;
}
    .zd-imgChange-controll:hover {
        background: rgba(0,0,0,0.5);
        opacity: 1;
    }
    .zd-imgChange-controll span {
        -moz-transition: color 0.4s;
        -o-transition: color 0.4s;
        -webkit-transition: color 0.4s;
        transition: color 0.4s;
    }
        .zd-imgChange-controll span:hover {
            color: rgba(0,0,0,0.6);
        }

HTML代码部分:

代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="viewport" content="width=device-width">
    <title>图片切换</title>
    <script src="jquery-1.10.2.min.js"></script>
    <link href="zoeDylan.ImgChange.css" rel="stylesheet" />
    <script src="zoeDylan.ImgChange.js"></script>
    <script>
            var a_imgs = new Array(//插入图片地址
                './1 (1).jpg',
                './1 (2).jpg',
                './1 (3).jpg',
                './1 (4).jpg'
                ),
                a_links = new Array(//点击图片跳转的网址
                'www.baidu.com',
                'www.qq.com',
                'www.google.com',
                'www.zol.com'
                ),
                a_tips = new Array(//鼠标停靠的提示
                '百度',
                '腾讯',
                '谷歌',
                '中关村'
                );
            $(function () {
            $('#imgc').zoeDylan_ImageChange({//设置
                width: 500,
                height: 300,
                imgs: a_imgs,
                links: a_links,
                tips: a_tips,
                timers:2000
            });
            });
    </script>
</head>
<body>
    <div id="imgc">
<!--        <span class="zd-imgChange-change zd-imgChange-change-left"><</span>
        <div class="zd-imgChange-img">
            <img class="zd-imgChange-img-item" src="http://subject.9ria.com/html5game/images/t1f.jpg" alt="图片加载错误!" title="提示" />
        </div>
        <div class="zd-imgChange-controll"><span>d</></div>
        <span class="zd-imgChange-change zd-imgChange-change-right">></span>-->
    </div>
</body>
</html>

ps:尽量使用高版本浏览器,目测测试浏览器:IE11 IE10 IE6 google 猎豹 欧鹏 可以完美运行,IE6透明效果有损失

代码下载:http://xiazai.jb51.net/201412/yuanma/zeodylanimgchange(jb51.net).rar

(0)

相关推荐

  • jquery简单图片切换显示效果实现方法

    本文实例讲述了jquery简单图片切换显示效果实现方法,分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>jquery图片切换效果</title> <script type="text/javascr

  • jQuery插件Slider Revolution实现响应动画滑动图片切换效果

    这是一款非常强大的内容切换插件,它基于jQuery,它充分响应,支持移动设备,支持手机触摸,键盘翻页:它内置幻灯.视频播放计时器,它拥有各种模式:自定义,自动响应,全屏:它有多种动画效果.3d效果...总之你想到的效果它都做到了,它的名字叫Slider Revolution. HTML Slider Revolution是一款基于jQuery的插件,使用它时需要先载入jQuery库文件,以及Slider Revolution依赖的css和js文件. <script src="js/jque

  • jQuery插件slick实现响应式移动端幻灯片图片切换特效

    jQuery响应式手机端移动端幻灯片图片切换特效插件slick,基于jQuery,功能非常强大,支持左右按钮切换.支持圆点切换.支持自定义切换数量,支持自定义切换速度.支持图片预加载.支持自动播放定义,效果非常的不错,众多的参数自定义支持,觉得可以的可以参考他们的参数配置,还是值得学习使用的. 使用方法: 1.加载插件和jQuery <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> &l

  • jquery实现图片切换代码

    本文实例为大家分享了jquery实现图片切换的方法,供大家参考,具体内容如下 <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title></title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.js&qu

  • jquery简单实现图片切换效果的方法

    本文实例讲述了jquery简单实现图片切换效果的方法.分享给大家供大家参考.具体实现方法如下: <!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">

  • jquery图片切换实例分析

    本文实例讲述了jquery图片切换实现方法.分享给大家供大家参考.具体如下: <!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

  • jQuery实现的Tab滑动选项卡及图片切换(多种效果)小结

    本文实例讲述了jQuery实现的Tab滑动选项卡及图片切换效果.分享给大家供大家参考.具体如下: 这里汇总了几个Tab,滑动门,选项卡,图片切换,在一个网页中实现了超多的常用效果,大家喜欢的滑动门,焦点图切换,标签选项卡以及文字轮番等都集中在了一起,无聊的功劳,忙的时候还顾不上写,另外还加入了圆角,都是参考以前学习的知识写的.期间使用了jquery-1.6.2.min.js框架库. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-n

  • 完美JQuery图片切换效果的简单实现

    效果如下: css: body { font-family:"Microsoft Yahei"; } body,ul,li,img,h3,dl,dd,dt,h1{margin:0px;padding:0px;list-style:none;} img{vertical-align: top;} /***大图切换***/ .scroll_view{margin: 0px auto;overflow:hidden;position: relative;} .photo_view li{po

  • 基于jQuery实现的幻灯图片切换

    话不多说,直接附上源码,仅供大家参考 // JavaScript Document ;(function($){ $.fn.extend({ "zj_ppt":function(value){ //默认参数定义 var $this = $(this); value = $.extend({ "time":2000, //间隔变化动画时间 "con":0, "sto":true, "count":"

  • 基于jquery实现鼠标滚轮驱动的图片切换效果

    jQuery可以制作出与Flash媲美的动画效果,这点绝对毋庸置疑,本文将通过实例演示一个基于鼠标滚轮驱动的图片切换效果. 本例实现的效果: 鼠标滚轮滚动时图片进行切换. 支持键盘方向键实现图片切换效果. 支持点击图片切换,支持点击当前图片链接. 进度条滑块展示图片图片数量进度. XHTML <div class="demo"> <div id="imageflow"> <div id="loading">&l

  • 基于jquery实现左右按钮点击的图片切换效果

    jQuery可以制作出与Flash媲美的动画效果,这点绝对毋庸置疑,本文将通过实例演示一个左右按钮点击的图片切换效果. 一.最终效果 二.功能分析 1.需求分析 点击左边pre按钮,显示前面三个图片,点击右边的next按钮,显示后面的一组(三个)图片.初始化只显示next按钮,到最后一组只显示pre按钮,中间过程两按钮都显示. 2.html结构分析 <div class="activity" id="activity-slide"> <a href

随机推荐