• javascript 实现滚动效果 整理


    1.先写两个最常用最简洁的滚动代码

    代码如下:
    水平滚动:
    <marquee direction="left" align="bottom" height="25" width="100%" onmouseout="this.start()" onmouseover="this.stop()" scrollamount="2" scrolldelay="1">水平滚动字幕内容</marquee>
    垂直滚动:
    <marquee direction="up" height="200" onmouseout="this.start()" onmouseover="this.stop()" scrollAmount="1" scrollDelay="1">垂直滚动字内容</marquee>

    2.平稳不间断滚动

     <SCRIPT   LANGUAGE="JavaScript">

     var   tm=null  
      function   newsScroll()   {
      var   scrollimg=document.getElementById("scroll") 
      if(scrollimg.parentNode.scrollTop!=(scrollimg.clientHeight/2))  
      scrollimg.parentNode.scrollTop++;  
      else 
      scrollimg.parentNode.scrollTop=0
      }  
      window.onload=function()   {  
      tm=setInterval('newsScroll()',25)    
      }  
      function   stop()  
      {  
      clearInterval(tm)  
      }  
       
      function   start()  
      {  
      tm=setInterval('newsScroll()',25)    
      }  

      </SCRIPT> 

    /* stop() start() 不能和调用ID在同一个容器内, 如容器没默认高度 要制定高度*/

    3 有停留滚动

    代码示例:

    代码
    <!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=gb2312" />
    <title>无标题文档</title>
    <style type="text/css">
    ul,li
    {margin:0;padding:0}
    #scrollDiv
    {width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}
    #scrollDiv li
    {height:25px;padding-left:10px;}
    </style>
    <script  src="jquery-1.3.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    (
    function($){
    $.fn.extend({
            Scroll:
    function(opt,callback){
                    
    //参数初始化
                    if(!opt) var opt={};
                    
    var _btnUp = $("#"+ opt.up);//Shawphy:向上按钮
                    var _btnDown = $("#"+ opt.down);//Shawphy:向下按钮
                    var timerID;
                    
    var _this=this.eq(0).find("ul:first");
                    
    var     lineH=_this.find("li:first").height(), //获取行高
                            line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), //每次滚动的行数,默认为一屏,即父容器高度
                            speed=opt.speed?parseInt(opt.speed,10):500//卷动速度,数值越大,速度越慢(毫秒)
                            timer=opt.timer //?parseInt(opt.timer,10):3000; //滚动的时间间隔(毫秒)
                    if(line==0) line=1;
                    
    var upHeight=0-line*lineH;
                    
    //滚动函数
                    var scrollUp=function(){
                            _btnUp.unbind(
    "click",scrollUp); //Shawphy:取消向上按钮的函数绑定
                            _this.animate({
                                    marginTop:upHeight
                            },speed,
    function(){
                                    
    for(i=1;i<=line;i++){
                                            _this.find(
    "li:first").appendTo(_this);
                                    }
                                    _this.css({marginTop:
    0});
                                    _btnUp.bind(
    "click",scrollUp); //Shawphy:绑定向上按钮的点击事件
                            });

                    }
                    
    //Shawphy:向下翻页函数
                    var scrollDown=function(){
                            _btnDown.unbind(
    "click",scrollDown);
                            
    for(i=1;i<=line;i++){
                                    _this.find(
    "li:last").show().prependTo(_this);
                            }
                            _this.css({marginTop:upHeight});
                            _this.animate({
                                    marginTop:
    0
                            },speed,
    function(){
                                    _btnDown.bind(
    "click",scrollDown);
                            });
                    }
                   
    //Shawphy:自动播放
                    var autoPlay = function(){
                            
    if(timer)timerID = window.setInterval(scrollUp,timer);
                    };
                    
    var autoStop = function(){
                            
    if(timer)window.clearInterval(timerID);
                    };
                     
    //鼠标事件绑定
                    _this.hover(autoStop,autoPlay).mouseout();
                    _btnUp.css(
    "cursor","pointer").click( scrollUp ).hover(autoStop,autoPlay);//Shawphy:向上向下鼠标事件绑定
                    _btnDown.css("cursor","pointer").click( scrollDown ).hover(autoStop,autoPlay);

            }       
    })
    })(jQuery);

    $(document).ready(
    function(){
            $(
    "#scrollDiv").Scroll({line:1,speed:500,timer:1000,up:"btn1",down:"btn2"});
    });
    </script>
    </head>

    <body>
    <p>多行滚动演示:</p>
    <div id="scrollDiv">
    <ul>
        
    <li>这是公告标题的第一行</li>
        
    <li>这是公告标题的第二行</li>
        
    <li>这是公告标题的第三行</li>
        
    <li>这是公告标题的第四行</li>
        
    <li>这是公告标题的第五行</li>
        
    <li>这是公告标题的第六行</li>
        
    <li>这是公告标题的第七行</li>
        
    <li>这是公告标题的第八行</li>
    </ul>
    </div>

    <span id="btn1">向前</span>&nbsp;&nbsp;<span id="btn2">向后</span>

    </body>
    </html>
     
     
     

    4:一个简单的滚动代码学习制作示例

    无间断循环滚动效果
    <!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>
                
    <title>无间断循环滚动效果</title>
                
    <meta http-equiv="content-type" content="text/html;charset=gb2312">
                
    <style type="text/css">
                .scroll
    {
                height
    :20px;
                overflow
    :hidden;
                font-size
    :12px;
                line-height
    :20px;
                border
    :#A5A5A5 1px dotted;
                width
    :120px;
                margin
    :0px auto;
                text-align
    :center;
                
    }
                
    </style>
                
    <script language="javascript" type="text/javascript">
                
    var s,sn=0,timer,slen,timer2;
                
    function scrollInit(){
                s
    =getid("s1");
                s.scrollTop
    =0;
                slen
    =s.innerHTML.split("|");
                s.innerHTML
    ="";
                
    for(var i=0;i<slen.length;i++){s.innerHTML+=(slen[i]+"<br />");}
                s.innerHTML
    +=slen[0];
                timer2
    =setInterval(scrollstart,3000);
                s.onmouseover
    =function(){clearInterval(timer2);clearInterval(timer);s.style.backgroundColor="#ccc";}
                s.onmouseout
    =function(){timer2=setInterval(scrollstart,3000);s.style.backgroundColor="#fff";}
                }
                
    function scrollstart(){
                
    if(s.scrollTop>=(slen.length*20)){s.scrollTop=0;}
                timer
    =setInterval(scrollexec,30);
                }
                
    function scrollexec(){
                
    if(sn<20){
                sn
    ++;
                s.scrollTop
    ++;
                }
    else{
                sn
    =0;
                clearInterval(timer);
                }
                }
                
    function getid(id){return document.getElementById(id);}
                window.onload
    =scrollInit;
                
    </script>
                
    </head>
                
    <body>
                
    <div id="s1" class="scroll">北 京|上 海|广 州|西 安|南 京|大 连|哈尔滨|武 汉</div>
                
    </body>
                
    </html>

     Js代码说明:

    JavaScript代码:
    var s,sn=0,timer,slen,timer2;
    //初始化设置
    function scrollInit(){   
        s=getid("s1"); //获取需要滚动内容的对象
        s.scrollTop=0; //初始化滚动位置
        slen=s.innerHTML.split("|"); //获取滚动内容,并存入数组,以便显示调用
        s.innerHTML=""; //清空滚动对象内容
        for(var i=0;i<slen.length;i++){s.innerHTML+=(slen[i]+"<br />");} //格式化输出滚动内容
        s.innerHTML+=slen[0];
        timer2=setInterval(scrollstart,3000); //开始滚动
        s.onmouseover=function(){clearInterval(timer2);clearInterval(timer);s.style.backgroundColor="#ccc";} //鼠标经过,停止滚动,改变样式
        s.onmouseout=function(){timer2=setInterval(scrollstart,3000);s.style.backgroundColor="#fff";} //鼠标移开,恢复滚动
    }
    //开启滚动效果
    function scrollstart(){
        if(s.scrollTop>=(slen.length*20)){s.scrollTop=0;} //滚动内容到底时,重置内容位置
        timer=setInterval(scrollexec,30); //执行滚动动画效果
        }
    //滚动动画效果
    function scrollexec(){
        if(sn<20){
            sn++;
            s.scrollTop++;
            }else{
                sn=0;
                clearInterval(timer);
                }       
        }
    //通过ID获取对象
    function getid(id){return document.getElementById(id);}
    //网页加载后,执行初始化
    window.onload=scrollInit;
    原理解析:
    1、首先给容器设定高度或宽度,比如div,设置div高20px;overflow:hidden;
    2、容器高度设定后,内容的高度超出20px,超过部分溢出,被隐藏,scrollTop属性可用,这一点可以用overflow:scroll来看效果;
    3、改变容器的scrollTop(上下滚动)或scrollLeft(左右滚动)属性的值,让内容移动位置(滚动的原理);
    4、到滚动的高度scrollTop大于或等于内容的高度时,设置scrollTop=0,让内容返回原来的位置,重新开始滚动,无间断循环滚动效果就出现了。

  • 相关阅读:
    JZ初中OJ 2266. 古代人的难题
    JZ初中OJ 1341. [南海2009初中] water
    JZ初中OJ 1340. [南海2009初中] jumpcow
    JZ初中OJ 2000. [2015.8.6普及组模拟赛] Leo搭积木
    JZ初中OJ 1999.[2015.8.6普及组模拟赛] Wexley接苹果
    Unity Android平台下插件/SDK开发通用流程
    UNITY接入支付宝(未测试可行)
    Unity接入支付宝(免写安卓代码,使用JAR方式)
    Unity之多态
    unity与android交互(1)与(2)网友的整理
  • 原文地址:https://www.cnblogs.com/syuhai/p/1689053.html
Copyright © 2020-2023  润新知