Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 216|回复: 0

[默认分类] JavaScript 图片滑动展示效果

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-7-4 11:50:16 | 显示全部楼层 |阅读模式
    更新版本:SlideView 图片滑动(扩展/收缩)展示效果

    看到jQuery实例:图片展示效果后,我也想拿来试试,但我不太喜欢用框架,所以自己做了个。
    其中的难点在于怎么设计各个滑动对象进行想要的滑动。
    一开始我想的是利用滑动对象的宽度的变化来实现滑动,
    但实行起来发现这个只能在理想状态下实现,一般实现起来很困难。
    所以还是参照了实例中的方法,利用left的变化来实现滑动。

    效果:

    var $$ = function (id) {return "string" == typeof id ? document.getElementById(id) : id;};function Event(e){var oEvent = document.all ? window.event : e;if (document.all) {if(oEvent.type == "mouseout") {oEvent.relatedTarget = oEvent.toElement;}else if(oEvent.type == "mouseover") {oEvent.relatedTarget = oEvent.fromElement;}}return oEvent;}function addEventHandler(oTarget, sEventType, fnHandler) {if (oTarget.addEventListener) {oTarget.addEventListener(sEventType, fnHandler, false);} else if (oTarget.attachEvent) {oTarget.attachEvent("on" + sEventType, fnHandler);} else {oTarget["on" + sEventType] = fnHandler;}};var Class = {create: function() {return function() {this.initialize.apply(this, arguments);}}}Object.extend = function(destination, source) {for (var property in source) {destination[property] = source[property];}return destination;}var GlideView = Class.create();GlideView.prototype = {//容器对象 容器宽度 展示标签 展示宽度initialize: function(obj, iWidth, sTag, iMaxWidth, options) {var oContainer = $$(obj), oThis=this, len = 0;this.SetOptions(options);this.Step = Math.abs(this.options.Step);this.Time = Math.abs(this.options.Time);this.Showtext = false;this._list = oContainer.getElementsByTagName(sTag);len = this._list.length;this._count = len;this._width = parseInt(iWidth / len);this._width_max = parseInt(iMaxWidth);this._width_min = parseInt((iWidth - this._width_max) / (len - 1));this._timer = null;if(this.options.TextTag){this.Showtext = true;this._text = oContainer.getElementsByTagName(this.options.TextTag);this._height_text = -parseInt(this.options.TextHeight);}this.Each(function(oList, oText, i){oList._target = this._width * i;//自定义一个属性放目标leftoList.style.left = oList._target + "px";oList.style.position = "absolute";addEventHandler(oList, "mouseover", function(){ oThis.Set.call(oThis, i); });if(oText){oText._target = this._height_text;//自定义一个属性放目标bottomoText.style.bottom = oText._target + "px";oText.style.position = "absolute";}})oContainer.style.width = iWidth + "px";oContainer.style.overflow = "hidden";oContainer.style.position = "relative";addEventHandler(oContainer, "mouseout", function(e){//变通防止执行oList的mouseoutvar o = Event(e).relatedTarget;if (oContainer.contains ? !oContainer.contains(o) : oContainer != o && !(oContainer.compareDocumentPosition(o) & 16)) oThis.Set.call(oThis, -1);})},//设置默认属性SetOptions: function(options) {this.options = {//默认值Step:   20,//滑动变化率Time:   20,//滑动延时TextTag:  "",//说明容器tagTextHeight:  0//说明容器高度};Object.extend(this.options, options || {});},//Set: function(index) {if (index  0 ? 1 : -1);return iStep;},Each:function(fun) {for (var i = 0; i
    #idGlideView{height:100px;}#idGlideView div{width:500px;height:100px;}#idGlideView div a{width:500px;height:50px;filter: alpha(opacity=50);opacity: 0.5; background:#000; color:#fff; text-decoration:none;}#idGlideView2 {height:380px;border:1px solid #000;}#idGlideView2 div {width:500px;height:380px;}


      1111111


      2222222


      3333333


      1111111


      2222222


      3333333






    不展开


































    var gv = new GlideView("idGlideView", 700, "div", 400, { TextTag: "a", TextHeight: 50 });var oSel = $$("sel");for (var i = 0; i = 500) gv.Step = 50; };$$("stop").onclick = function(){ clearTimeout(gv._timer); };$$("start").onclick = function(){ gv.Move(); };$$("close").onclick = function(){ gv.Step = 1; };$$("open").onclick = function(){ gv.Step = gv.options.Step; };$$("hide").onclick = function(){ gv.Showtext = false; };$$("show").onclick = function(){ gv.Showtext = true; };var t = null, i = -1;$$("auto").onclick = function(){ clearInterval(t);t=setInterval(function(){ if(++i>gv._count) i=0; gv.Set(i); }, 1000); };$$("cancel").onclick = function(){ clearInterval(t);gv.Set(-1); };




    程序原理:
    设计之初,先不要考虑文本显示那部分,把重心放到滑动效果的实现上,
    减少设计负担,这是小小的技巧吧。
    总体的思路是通过移动各个滑动对象到指定的位置来实现不同的效果。
    效果有两种,分部是:鼠标移出容器时的默认效果和鼠标移到某个滑动对象时的展示效果。
    根据效果可以看出,滑动对象有三种宽度一个是默认宽度,最大宽度和最小宽度。
    这些宽度都可以在初始化时设置:


    Code

       this._list = oContainer.getElementsByTagName(sTag);
    len = this._list.length;
    this._count = len;
    this._width = parseInt(iWidth / len);
    this._width_max = parseInt(iMaxWidth);
    this._width_min = parseInt((iWidth - this._width_max) / (len - 1));

    我给每个滑动对象添加了一个_target属性来放一个目标值,用来告诉它要滑到哪里。
    移动之前先执行Set()程序,给每个对象设置目标值,要分两种情况:
    1.鼠标移出容器:这时只要设置滑动对象的目标值为默认宽度*索引值就行;


    oList._target = this._width * i;

    2.鼠标移到某个滑动对象上:把当前对象和之前的滑动对象的目标值设为最小宽度*索引值,之后的对象设为最小宽度*(索引值-1)加上最大宽度(因为这样会算多一个最小宽度所以要减1个),这样设置就能得到要实现的效果:


    oList._target = (i <= index) ? this._width_min * i : this._width_min * (i - 1) + this._width_max;

    接下来就是怎么移动到目标值了,这个在Move()程序中实现。
    首先移动效果是通过用计时器逐步设置滑动对象的left来实现,减速部分用一个GetStep()程序来实现(相关说明请看JavaScript 图片切换展示效果)。
    不断移动,直到所有滑动对象都到达目标值:


    Code

       clearTimeout(this._timer);
    var bFinish = true;
    this.Each(function(oList, oText, i){
        var nowLeft = parseInt(oList.style.left), iLeftStep = this.GetStep(oList._target, nowLeft);
        if (iLeftStep != 0) { bFinish = false; oList.style.left = (nowLeft + iLeftStep) + "px"; }
    })
    if (!bFinish) { var oThis = this; this._timer = setTimeout(function(){ oThis.Move(); }, this.Time); }

    这个程序本身的难度不高,就难在程序设计,例如怎么实现变化的效果(由于有实例难度就没那么高了)。
    还有一个思想是把“移动到指定目标”这个任务分派到各个滑动对象,
    主程序只要知道是否各个滑动对象都到达指定目标就行了,
    这在给滑动对象设置_target属性和Each()程序的应用中能体现出来。
    如果每次移动都由主程序来计算(试想想没有这个_target属性),难度会大大增加,
    而设计之初很容易会陷入这个死胡同。
    还有一点是容器的mouseout事件中,要先判断鼠标是否在容器外(相关说明请看JavaScript 自定义多级联动浮动菜单)。

    扩展功能:
    可以设置的属性:
    Step: 滑动变化率;
    Time: 滑动延时;
    TextTag: 说明容器tag;
    TextHeight: 说明容器高度;
    Delay: 延迟值(微秒);
    Showtext:是否显示说明文本;
    能实现什么功能就看各位的想象力了。

    程序测试:
    实例化对象:


    new GlideView("idGlideView", 1000, "div", 500, { TextTag: "a", TextHeight: 50 });

    其中参数分别是容器对象,容器宽度,展示标签,展示宽度,最后是一些设置。

    程序代码:


    Code

       var $ = function (id) {
        return "string" == typeof id ? document.getElementById(id) : id;
    };

    function Event(e){
        var oEvent = document.all ? window.event : e;
        if (document.all) {
            if(oEvent.type == "mouseout") {
                oEvent.relatedTarget = oEvent.toElement;
            }else if(oEvent.type == "mouseover") {
                oEvent.relatedTarget = oEvent.fromElement;
            }
        }
        return oEvent;
    }

    function addEventHandler(oTarget, sEventType, fnHandler) {
        if (oTarget.addEventListener) {
            oTarget.addEventListener(sEventType, fnHandler, false);
        } else if (oTarget.attachEvent) {
            oTarget.attachEvent("on" + sEventType, fnHandler);
        } else {
            oTarget["on" + sEventType] = fnHandler;
        }
    };

    var Class = {
      create: function() {
        return function() {
          this.initialize.apply(this, arguments);
        }
      }
    }

    Object.extend = function(destination, source) {
        for (var property in source) {
            destination[property] = source[property];
        }
        return destination;
    }


    var GlideView = Class.create();
    GlideView.prototype = {
      //容器对象 容器宽度 展示标签 展示宽度
      initialize: function(obj, iWidth, sTag, iMaxWidth, options) {
        var oContainer = $(obj), oThis=this, len = 0;
       
        this.SetOptions(options);
       
        this.Step = Math.abs(this.options.Step);
        this.Time = Math.abs(this.options.Time);
        this.Showtext = false;//是否显示说明文本
       
        this._list = oContainer.getElementsByTagName(sTag);
        len = this._list.length;
        this._count = len;
        this._width = parseInt(iWidth / len);
        this._width_max = parseInt(iMaxWidth);
        this._width_min = parseInt((iWidth - this._width_max) / (len - 1));
        this._timer = null;
       
        //有说明文本
        if(this.options.TextTag && this.options.TextHeight > 0){
            this.Showtext = true;
            this._text = oContainer.getElementsByTagName(this.options.TextTag);
            this._height_text = -parseInt(this.options.TextHeight);
        }
       
        this.Each(function(oList, oText, i){
            oList._target = this._width * i;//自定义一个属性放目标left
            oList.style.left = oList._target + "px";
            oList.style.position = "absolute";
            addEventHandler(oList, "mouseover", function(){ oThis.Set.call(oThis, i); });
            
            //有说明文本
            if(oText){
                oText._target = this._height_text;//自定义一个属性放目标bottom
                oText.style.bottom = oText._target + "px";
                oText.style.position = "absolute";
            }
        })
       
        //容器样式设置
        oContainer.style.width = iWidth + "px";
        oContainer.style.overflow = "hidden";
        oContainer.style.position = "relative";
        //移出容器时返回默认状态
        addEventHandler(oContainer, "mouseout", function(e){
            //变通防止执行oList的mouseout
            var o = Event(e).relatedTarget;
            if (oContainer.contains ? !oContainer.contains(o) : oContainer != o && !(oContainer.compareDocumentPosition(o) & 16)) oThis.Set.call(oThis, -1);
        })
      },
      //设置默认属性
      SetOptions: function(options) {
        this.options = {//默认值
            Step:            20,//滑动变化率
            Time:            20,//滑动延时
            TextTag:        "",//说明容器tag
            TextHeight:        0//说明容器高度
        };
        Object.extend(this.options, options || {});
      },
      //相关设置
      Set: function(index) {
        if (index < 0) {
            //鼠标移出容器返回默认状态
            this.Each(function(oList, oText, i){ oList._target = this._width * i; if(oText){ oText._target = this._height_text; } })
        } else {
            //鼠标移到某个滑动对象上
            this.Each(function(oList, oText, i){
                oList._target = (i <= index) ? this._width_min * i : this._width_min * (i - 1) + this._width_max;
                if(oText){ oText._target = (i == index) ? 0 : this._height_text; }
            })
        }
        this.Move();
      },
      //移动
      Move: function() {
        clearTimeout(this._timer);
        var bFinish = true;//是否全部到达目标地址
        this.Each(function(oList, oText, i){
            var iNow = parseInt(oList.style.left), iStep = this.GetStep(oList._target, iNow);
            if (iStep != 0) { bFinish = false; oList.style.left = (iNow + iStep) + "px"; }
            //有说明文本
            if (oText) {
                iNow = parseInt(oText.style.bottom), iStep = this.GetStep(oText._target, iNow);
                if (iStep != 0) { bFinish = false; oText.style.bottom = (iNow + iStep) + "px"; }
            }
        })
        //未到达目标继续移动
        if (!bFinish) { var oThis = this; this._timer = setTimeout(function(){ oThis.Move(); }, this.Time); }
      },
      //获取步长
      GetStep: function(iTarget, iNow) {
        var iStep = (iTarget - iNow) / this.Step;
        if (iStep == 0) return 0;
        if (Math.abs(iStep) < 1) return (iStep > 0 ? 1 : -1);
        return iStep;
      },
      Each:function(fun) {
        for (var i = 0; i < this._count; i++)
            fun.call(this, this._list, (this.Showtext ? this._text : null), i);
      }
    };

    下载完整测试代码
    其他图片展示效果:
    JavaScript 图片变换效果(ie only)
    JavaScript 图片切换展示效果
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-24 07:46 , Processed in 0.334749 second(s), 35 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表