zoukankan      html  css  js  c++  java
  • js 运动框架及实例

    <!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>
        <script src="move.js" type="text/javascript"></script>
        <script type="text/javascript">
            window.onload = function(){
                var oDiv = document.getElementById("div1");
                var oBtn = document.getElementById("btn1");
                
                oBtn.onclick = function(){
                     move(oDiv,'right',300);   //可使
                     //move(oDiv,'left',300);  //可使
                     //move(oDiv,'top',300);   //可使
                     //move(oDiv,'width',300); //可使
                     //move(oDiv,'height',300); //可使
                }
                
                oDiv.onmouseover = function(){
                    move(oDiv,'opacity',100);
                }
                
                oDiv.onmouseout = function(){
                    move(oDiv,'opacity',30);
                }
    
            }
        </script>
    </head>
    <body>
      <input type="button" id="btn1" value="运动" />
       <div id="div1" style="200px;height:200px;position:absolute;background:red;filter:alpha(opacity=30);opacity:0.3;">
       </div>
    </body>
    </html>

    js:

    //height,left , top ,right ,opacity
    var timer = null 
    function move(obj,attr,target)
    {
         //开启定时器
         timer = setInterval(function(){
         var current = 0;
         if(attr=="opacity")
         {
            current = Math.round(parseFloat(getStyle(obj,attr))*100);
         }
         else
         {
            current = parseInt(getStyle(obj,attr));
         }
         
        var speed = target>current ? 6 :-6;
        
        if(Math.abs(current-target)<6)
        {
            //清除定时器
            clearInterval(timer);
        }
        else
        {
             if(attr=="opacity")
             {
                obj.style.filter = "alpha(opacity:"+(current + speed) +")";
                obj.style.opacity = (current + speed) /100;
                
             }
             else
             {
                obj.style[attr] = current + speed +"px";
             }
        }
        
       },30);
    
    }
    
    function getStyle(obj,attr)
    {
        if(obj.currentStyle)  //用于IE
        {
            return obj.currentStyle[attr];
        }
        else
        {
            //document.defaultView.getComputedStyle 该方法时DOM2中才出现的方法
            return document.defaultView.getComputedStyle(obj, null)[attr];
            //getComputedStyle DOM1中的方法
            //return getComputedStyle(obj,false)[attr];
        }
    }
            
  • 相关阅读:
    总结ASP.NET C#中经常用到的13个JS脚本代码
    C# 处理 JSON 常用的帮助类
    C# 中移动文件到指定位置
    C# 常用时间戳处理方法
    c#批量上传图片到服务器示例分享
    C# 通过URL获取图片并显示在PictureBox上的方法
    QT中Qtableview视图表格中点击表头进行排序
    qt QTableWidget&&QTableView 导出数据到excel
    51nod 1242 斐波那契数列的第N项
    矩阵快速幂
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/4329371.html
Copyright © 2011-2022 走看看