纯javascript图片渐变切换

稿件来源: 阳光企业网站管理系统   撰稿作者: 太阳光   发表日期: 2013-05-29   阅读次数: 651   查看权限: 游客查看

在jquery只要写个函数就可实现图片渐变效果。如果纯js实现又是怎样的呢。

图片渐变效果,使用定时器与改变css透明度即可。 点击查看演示

<!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>纯javascript图片渐变切换</title>
    <style type="text/css">
    #pic_div{
        width:600px;
        height: 400px;
        position: relative;
        border: 1px solid red;
        overflow: hidden;
    }
    .pic{
        position: absolute;
        top:0;
        z-index: 1;
    }
    .pic img{
        border: 0;width:600px;
        height: 400px;
    }
    #pic_ul{
        margin: 0;padding:0;
        list-style: none;
        position: absolute;
        z-index: 4;
        top:370px;
        left:230px;
    }
    #pic_ul li{
        margin: 0;padding:0;
        list-style: none;
        font: 10px/20px arial;
        background-color: #eeff53;
        color: #333;
        width:20px;height:20px;
        float: left;margin-left: 5px;
        text-align: center;
        cursor: pointer;
        border-radius: 10px;
    }
    #pic_ul .on{
        background-color: red;
        color: #fff;
    }
    </style>
</head>
<body>

<div id="pic_div">
    <div class="pic"><img  src ="http://www.scscms.com/FUploadFile/Image/2010-9/1.jpg" /></div>
    <div class="pic"><img  src ="http://www.scscms.com/FUploadFile/Image/2010-9/2.jpg" /></div>
    <div class="pic"><img  src ="http://www.scscms.com/FUploadFile/Image/2010-9/3.jpg" /></div>
    <div class="pic"><img  src ="http://www.scscms.com/FUploadFile/Image/2010-9/5.jpg" /></div>
    <ul id="pic_ul">
        <li><<</li>
        <li class="on">1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>>></li>
    </ul>
</div>
<script type="text/javascript">
    var n=0;//播放第几张
    var o=1;//之前播放的编号
    var s= 0,t=4000;//时间间隔
    var setT,setP=null;//定时器
    var pic=document.getElementById("pic_div").getElementsByTagName("div");//图片所在div
    var li=document.getElementById("pic_ul").getElementsByTagName("li");//图片对应li
    function change_pic(a){
        clearInterval(setT);
        if(a<0)a=pic.length-1;
        if(a>=pic.length)a=0;
        for(var l=0;l<li.length;l++){
            li[l].className="";
        }
        for(var i=0;i<pic.length;i++){
            if(a==i){
                pic[i].style.opacity=0;
                pic[i].style.filter="alpha(opacity=0)";
                pic[i].style.zIndex=3;
            }else{
                pic[i].style.opacity=1;
                pic[i].style.filter="alpha(opacity=100)";
                if(i==o){
                    pic[i].style.zIndex=2;
                }else{
                    pic[i].style.zIndex=1;
                }
            }
        }
        li[a+1].className="on";
        s=0;//从0开始
        o=n=a;
        setT=setInterval(function(){
            s++;
            if(s>100){
                o=a;//完成动画后身份转变
                clearInterval(setT);
            }else{
                pic[a].style.opacity=s/100;
                pic[a].style.filter="alpha(opacity="+s+")";
            }
        },20)
    }
    for(var i=0;i<li.length;i++){
        li[i].onclick=(function(a){
            return function(){
                if(a==0){
                    change_pic(n-1);//上一张
                }else if(a==li.length-1){
                    change_pic(n+1);//下一张
                }else{
                    change_pic(a-1);//当前张
                }
            }
        })(i)
    }
    //定时播放控制
    setP=setInterval(function(){ n++;change_pic(n)},t);
    document.getElementById("pic_div").onmouseover=function(){clearInterval(setP)}
    document.getElementById("pic_div").onmouseout=function(){
        setP=setInterval(function(){ n++;change_pic(n)},t);
    }
</script>
</body>
</html>

 

关键词: javascript,图片切换,渐变   编辑时间: 2013-08-26 17:30:50

  • 感到高兴

    0

    高兴
  • 感到支持

    0

    支持
  • 感到搞笑

    0

    搞笑
  • 感到不解

    0

    不解
  • 感到谎言

    0

    谎言
  • 感到枪稿

    0

    枪稿
  • 感到震惊

    0

    震惊
  • 感到无奈

    0

    无奈
  • 感到无聊

    0

    无聊
  • 感到反对

    0

    反对
  • 感到愤怒

    0

    愤怒
0%(0)
0%(0)
共有0 条评论 发言请遵守【相关规定

网友评论

会员头像
发 表同步腾讯微博    验证码:  点击更新请先登陆
  • 暂无评论
关闭模块文章图片 article Pictrue
  • 我的妈妈爸爸
  • 基于koa2+mysql+vue2.0+Element阳光内容管理系统
  • 代码覆盖率工具 Istanbul 入门教程
  • 全栈工程师的武器——MEAN
  • 9款超炫的 CSS3 复选框(Checkbox)
  • 微信开发在线翻译功能
  • CSS3那些不为人知的高级属性
  • 给easyui的datebox添加清空事件
  • flash写字效果
  • kendoUI系列教程之DropDownList下拉菜单
  • kendoUI系列教程之datetimepicker日期时间选择
  • kendoUI系列教程之datepicker日期选择
  • kendoUI系列教程之combobox下拉列表框
  • kendoUI系列教程之colorpicker
  • kendoUI系列教程之calendar日历表
  • kendoUI系列教程之autocomplete自动补齐