Javscript轮播 支持平滑和渐隐两种效果(可以只有两张图)

简介: 原文:Javscript轮播 支持平滑和渐隐两种效果(可以只有两张图)先上两种轮播效果:渐隐和移动   效果一:渐隐 1 2 3 4 效果二:移动 1 2 3 4   接下来,我们来大致说下整个轮播的思路: 一、先来看简单的,移动的,先上来一个图----移动效果图: 说明:       基本原则就是顺序是按照当前显示的为基准:如当前为2,那么顺序就是2,3,4,1;如当前为3,那么顺序就是3,4,1,2。
原文: Javscript轮播 支持平滑和渐隐两种效果(可以只有两张图)

先上两种轮播效果:渐隐和移动
 
效果一:渐隐
1 2 3 4
效果二:移动
1 2 3 4

 

接下来,我们来大致说下整个轮播的思路:

一、先来看简单的,移动的,先上来一个图----移动效果图:

说明:

      基本原则就是顺序是按照当前显示的为基准:如当前为2,那么顺序就是2,3,4,1;如当前为3,那么顺序就是3,4,1,2。以此类推。

      整个移动划分为三种:1、下一个  2、上一个  3、任意个

     1、下一个:margin-left:-图宽;然后将“图1”移到最后一个位置

 

   next: function () {
            var oThis = this;
            var firstItem = oThis.itemArray.shift();
            oThis.itemArray.push(firstItem);
            rotatePrivate.clealNvActive.call(oThis, oThis.itemArray[0].index);
            //移动wrap到第二个元素
            oThis.wrap.animate({ marginLeft: -oThis.itemW }, {
                duration: oThis.actionTime,
                easing: 'easeInOutQuint',
                complete: function () {
                    //第一元素移到最后
                    oThis.wrap.append(firstItem.item);
                    oThis.wrap.css('margin-left', 0);
                    rotatePrivate.timeRun.call(oThis);
                }
            });
        },

 

 

 

      2、上一个:将最后一个(图4)移到第一个,设置margin-left:-图宽,然后动作设置成margin-left:0

 

  pre: function () {
            var oThis = this;
            //找到最后一张,并移到第一张
            var lastItem = oThis.itemArray.pop();
            oThis.wrap.prepend(lastItem.item);
            oThis.wrap.css('margin-left', -oThis.itemW);
            rotatePrivate.clealNvActive.call(oThis, lastItem.index);
            oThis.wrap.animate({ marginLeft: 0 }, {
                duration: oThis.actionTime,
                easing: 'easeInOutQuint',
                complete: function () {
                    //变化数组
                    oThis.itemArray.splice(0, 0, lastItem);
                    rotatePrivate.timeRun.call(oThis);
                }

            });
        },

 

 

 

      3、任意个:先判断向前移,还是向后移动,然后根据基本原则就是顺序是按照当前显示的为基准,从新排列顺序。

 

curstom: function (i) {
            var oThis = this;
            var customItem = null;
            for (var h in oThis.itemArray) {
                if (oThis.itemArray[h].index == i) {
                    customItem = oThis.itemArray[h];
                    break;
                }
            }
            var firstItem = oThis.itemArray[0];
            //在活动的后面
            if (customItem.index > firstItem.index) {
                //把curstomItem移到后面
                firstItem.item.after(customItem.item);
                rotatePrivate.clealNvActive.call(oThis, customItem.index);
                //foucus move to curstomitem
                oThis.wrap.animate({ marginLeft: -oThis.itemW }, {
                    duration: oThis.actionTime,
                    complete: function () {
                        //sort by customitem
                        rotatePrivate.sortItem.call(oThis, customItem);
                        oThis.wrap.css('margin-left', 0);
                        rotatePrivate.timeRun.call(oThis);
                    }
                });
            } else {
                //把curstomItem移到当前的前面,并margin-left -itemWidth
                firstItem.item.before(customItem.item);
                oThis.wrap.css('margin-left', -oThis.itemW);
                rotatePrivate.clealNvActive.call(oThis, customItem.index);
                //foucus move to curstomitem
                oThis.wrap.animate({ marginLeft: 0 }, {
                    duration: oThis.actionTime,
                    complete: function () {
                        //sort by customitem
                        rotatePrivate.sortItem.call(oThis, customItem);
                        rotatePrivate.timeRun.call(oThis);
                    }
                });
            }
        }

 

 

 

二、再来看渐隐轮播效果

这个在原来的效果上,唯一比较有亮点的就是“渐隐如何不让图片白一下”?

图1   图2  图3   图4

 

 

图1克隆

图2   图3  图4   图1

我采用clone了一张当前,并设置position: absolute;这样当当前这样的opacity变为0时,底下的图2就显示出来,这样就不那么生硬了。

 

    next: function () {
            var oThis = this;
            var firstItem = oThis.itemArray.shift();
            oThis.itemArray.push(firstItem);
            //将第一个clone一个,覆在上面
            var firstClone = firstItem.item.clone();
            firstClone.addClass('rotate-trans');
            firstClone.removeClass('rotate-item');
            oThis.wrap.append(firstClone);
            //first ele move to last
            oThis.wrap.append(firstItem.item);
            var secondItem = oThis.itemArray[0];
            rotatePrivate.clealNvActive.call(oThis, secondItem.index);
            firstClone.animate({ opacity: 0 }, {
                duration: oThis.actionTime,
                complete: function () {
                    //移走clone
                    firstClone.remove();
                    rotatePrivate.timeRun.call(oThis);
                }
            });
        },

 

 

 

 

目录
相关文章
|
8天前
|
索引
【sgPhotoPlayer】自定义组件:图片预览,支持点击放大、缩小、旋转图片
【sgPhotoPlayer】自定义组件:图片预览,支持点击放大、缩小、旋转图片
|
4月前
使用分面展示不同组别的双 Y 轴图形
使用分面展示不同组别的双 Y 轴图形
33 0
|
4月前
|
测试技术 定位技术
【sgDragMoveTile】自定义组件:拖拽瓦片图、地图、大图,滚动条对应同步滚动
【sgDragMoveTile】自定义组件:拖拽瓦片图、地图、大图,滚动条对应同步滚动
|
4月前
|
测试技术
【sgTileImage】自定义组件:瓦片图拖拽局部加载、实现以鼠标为中心缩放
【sgTileImage】自定义组件:瓦片图拖拽局部加载、实现以鼠标为中心缩放
|
4月前
|
数据可视化 大数据
【透明版九宫格背景图片】仅依靠background的几个属性组合搭配出酷炫的透明背景卡片效果→适用于大数据可视化、数据大屏展示页面
【透明版九宫格背景图片】仅依靠background的几个属性组合搭配出酷炫的透明背景卡片效果→适用于大数据可视化、数据大屏展示页面
【透明版九宫格背景图片】仅依靠background的几个属性组合搭配出酷炫的透明背景卡片效果→适用于大数据可视化、数据大屏展示页面
|
6月前
31Echarts - 柱状图(特性示例:渐变色 阴影 点击缩放)
31Echarts - 柱状图(特性示例:渐变色 阴影 点击缩放)
26 0
|
6月前
|
定位技术
ArcMap | 出图小技巧——比例尺、鹰眼图、表格、文本、图片
ArcMap | 出图小技巧——比例尺、鹰眼图、表格、文本、图片
146 0
|
9月前
|
定位技术
Echarts实战案例代码(39):地理坐标整体地图背景色渐变效果和字体随地图缩放的解决方案
Echarts实战案例代码(39):地理坐标整体地图背景色渐变效果和字体随地图缩放的解决方案
179 0
|
10月前
图片和文件预览组件(部分源码),可拖动,缩小,放大。 #41
图片和文件预览组件(部分源码),可拖动,缩小,放大。 #41
88 0
|
10月前
|
JavaScript 前端开发 容器
手写图片拖拽、鼠标点位缩放
如题,无关技术背景,什么vue、react自己想用就用,这就是js加一点点css实现,意思就是可以任意迁。
137 0