Heim > Web-Frontend > js-Tutorial > Hauptteil

Perfekt kompatibel mit jQuery-Plug-Ins für gängige Browser, um Bildwechseleffekte zu erzielen_jquery

WBOY
Freigeben: 2016-05-16 16:27:24
Original
1572 Leute haben es durchsucht

Die Funktionskommentare in der Datei sind ebenfalls sehr detailliert geschrieben (Einzelheiten finden Sie in der Datei zoeDylan.ImgChange-1.0.1.js), was für Internetnutzer sehr hilfreich ist, obwohl der Stil nicht sehr gut aussieht , Sie können es selbst schreiben. . .

JS-Codeteil:

Code kopieren Der Code lautet wie folgt:

(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 = '';
        _eleThis.append(_eleTemp);
        _eleTemp = '    
  
';
        _eleThis.append(_eleTemp);
        _eleTemp = '      
';
        _eleThis.append(_eleTemp);
        _eleTemp = ' ';
        _eleThis.append(_eleTemp);
        //给全局变量赋值
        _eleImg = $('.zd-imgChange-img');
        _eleControll = $('.zd-imgChange-controll');
        _eleChange = $('.zd-imgChange-change');
    }
    //添加图片
    Funktion 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('' _icon[1] '');
            //提示
            $(_eleImg.children('img')[i]).attr({ 'title': _setting.tips[i] '|' _setting.links[i] });
            $(_eleControll.children('span')[i]).attr('title', _setting.tips[i]);
        }
        Dispose(0, 'TtoB');
    }
    //处理尺寸
    Funktion 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' });
    }
    //处理操作事件
    Funktion 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');
                        } sonst {
                            Dispose(i - 1, 'RtoL');
                        }
                        Pause;
                    }
                }
            } 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');
                        } sonst {
                            Dispose(i 1, 'LtoR');
                        }
                        Pause;
                    }
                }
            } sonst {
                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');
                    }
                    Pause;
                }
            }
        });
    }
    //切换器
    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实现视觉效果
        Funktion 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);
            }
        }
    }
    //自动切换时钟
    Funktion 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');
                } sonst {
                    Dispose(i - 1, 'TtoB');
                }
                Pause;
            }
        }
    }
})($)

CSS代码部分:

复制代码 代码如下:

.zd-imgChange {
    Position: relativ;
    Rand: automatisch;
    Polsterung: 0px;
    Mindestbreite: 200 Pixel;
    Mindesthöhe: 100px;
    Hintergrund: rgba(0,0,0,0.5);
    Farbe: #fff;
    Cursor: Zeiger;
    Überlauf: versteckt;
}
.zd-imgChange-change {
    Cursor:Zeiger;
    Farbe:#fff;
    Rand: 0px;
    Polsterung: 0px;
    Position: relativ;
    Hintergrund: rgba(0,0,0,0.5);
    Breite: 10 %;
    Höhe: 100 %;
    text-align: center;
    Deckkraft: 0,1;
    Z-Index: 1;
    -moz-transition: Deckkraft 0,4 s;
    -o-transition: Deckkraft 0,4 s;
    -webkit-transition: Deckkraft 0,4 s;
    Übergang: Deckkraft 0,4 s;
    border:none;
}
    .zd-imgChange-change:hover {
        Deckkraft: 1;
    }
.zd-imgChange-change-left {
    float: links;
}
.zd-imgChange-change-right {
    float: rechts;
}
.zd-imgChange-img {
    Z-Index: 0;
    Polsterung: 0px;
    Position: absolut;
    oben: 0px;
    links: 0px;
    Hintergrund: rgba(100,0,0,0.1);
    Breite: 100 %;
    Höhe: 100 %;
}
.zd-imgChange-img-item {
    Position: absolut;
    oben: 0px;
    links: 0px;
    Breite: 100 %;
    Höhe: 100 %;
    Grenze: keine;
    -moz-background-size: cover;
    -o-background-size: cover;
    Hintergrundgröße: cover;
    -moz-transition: Deckkraft 0,5 s;
    -o-transition: Deckkraft 0,5 s;
    -webkit-transition: Deckkraft 0,5 s;
    Übergang: Deckkraft 0,5 s;
    Deckkraft: 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;
    Polsterung: 0px;
    Position: absolut;
    unten: 0px;
    Breite: 100 %;
    Höhe: 20 %;
    Hintergrund: rgba(0,0,0,0);
    text-align: center;
    -moz-transition: Hintergrund 0,4s;
    -o-Übergang: Hintergrund 0,4 s;
    -webkit-transition: Hintergrund 0,4 s;
    Übergang: Hintergrund 0,4s;
    Textschatten: 0px 0px 5px #000;
    Deckkraft: 0,7;
}
    .zd-imgChange-controll:hover {
        Hintergrund: rgba(0,0,0,0.5);
        Deckkraft: 1;
    }
    .zd-imgChange-controll span {
        -moz-transition: Farbe 0,4s;
        -o-Übergang: Farbe 0,4 s;
        -webkit-transition: Farbe 0,4 s;
        Übergang: Farbe 0,4s;
    }
        .zd-imgChange-controll span:hover {
            Farbe: rgba(0,0,0,0.6);
        }

HTML代码部分:

复制代码 代码如下:


http://www.w3.org/1999/xhtml">


Bildwechsel



<script><br>             var a_imgs = new Array(//Bildadresse einfügen<br> ‘./1 (1).jpg’,<br> ‘./1 (2).jpg’,<br> ‘./1 (3).jpg’,<br> ‘./1 (4).jpg’<br> ),<br> A_links = Neues Array (// Klicken Sie auf die URL des Bildsprungs <br>                   'www.baidu.com',<br>                  'www.qq.com',<br>                  'www.google.com',<br>                  'www.zol.com'<br> ),<br>                  a_tips = new Array(//Tipps zum Andocken der Maus<br>                'Baidu',<br> „Tencent“,<br> „Google“,<br>                 'Zhongguancun'<br> );<br>                $(function () {<br>                 $('#imgc').zoeDylan_ImageChange({//Settings<br>                      Breite: 500,<br> Höhe: 300,<br> imgs: a_imgs,<br>               Links: a_links,<br> Tipps: a_tips,<br>               Timer:2000<br>             });<br>               }); <br> </script>








ps: Versuchen Sie, eine höhere Version des Browsers zu verwenden, visueller Testbrowser: IE11 IE10 IE6 Google Cheetah Oupeng kann perfekt ausgeführt werden, der IE6-Transparenzeffekt geht verloren

Code-Download: http://xiazai.jb51.net/201412/yuanma/zeodylanimgchange(jb51.net).rar

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage