zoukankan      html  css  js  c++  java
  • js笔记---(运动)通用的move方法,兼容透明度变化

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <style>
            /*#div1{ height:100px; 100px; position:absolute; --left:800px; margin-top:90px; background-color:red;}
            span { height:300px; 1px; position:absolute; left:500px; background-color:black; }*/
            div { height:100px; 100px; margin-top:50px; background-color:red; }
        </style>
        <script type="text/javascript">
            window.onload = function () {
                //var oBtn = document.getElementById('btn1');
                //oBtn.onclick = function () {
                //    starMove(400);
                //}
                var oDiv = document.getElementsByTagName('div');
                //var i = 0;
                //for (i = 0; i < oDiv.length; i++) {
                    
                //}
                oDiv[0].onmouseover = function () {
                    starMove(this, 'width', 300);
                }
                oDiv[0].onmouseout = function () {
                    starMove(this, 'width', 100);
                }
                oDiv[1].onmouseover = function () {
                    starMove(this, 'height', 300);
                }
                oDiv[1].onmouseout = function () {
                    starMove(this, 'height', 100);
                }
                oDiv[2].onmouseover = function () {
                    starMove(this, 'opacity', 30);
                }
                oDiv[2].onmouseout = function () {
                    starMove(this, 'opacity', 100);
                }
            }
            window.onscroll = function () {
                var scroTop = document.documentElement.scrollHeight || document.body.scrollTop;
            }
            //获取样式数值
            function getStyle(obj, attr)
            {
                if (obj.currentStyle) {
                    return obj.currentStyle[attr];
                }
                else {
                    return getComputedStyle(obj, false)[attr];
                }
            }
            
            var iSpeed=0;
            function starMove(obj,attr,iTarget)
            {
                clearInterval(obj.timer);
                var iCur = 0;
                obj.timer = setInterval(function () {
                    //parseInt(parseFloat(getStyle(obj, attr)) * 100)中 parseFloat(getStyle(obj, attr)为取带小数的opacity值
                    //parseInt(...) 目的为 解决计算机小数问题bug 防止3.0000000000001=3 类似问题
                    iCur = attr == "opacity" ? parseInt(parseFloat(getStyle(obj, attr)) * 100) : parseInt(getStyle(obj, attr));
                    //iCur = parseInt(getStyle(obj, attr));
                    iSpeed =(iTarget - iCur) / 8;
                    iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
                    if (iCur == iTarget) {
                        clearInterval(obj.timer);
                    } else {
                        if (attr == "opacity") {
                            obj.style.filter = 'alpha(opacity=' + (iCur + iSpeed) + ')';
                            obj.style.opacity = (iCur + iSpeed) / 100;
                        } else {
                            obj.style[attr] = iCur + iSpeed + 'px';
                        }
                        
                    }
                }, 30);
            }
        </script>
    </head>
    <body>
        <!--<input type="button" id="btn1" value="开始" /><br />
        <div id="div1"></div>
        <span></span>-->
        <div></div>
        <div></div>
        <div></div>
    </body>
    </html>
    

      

  • 相关阅读:
    从运维角度看中大型网站架构的演变之路
    <经验杂谈>Mysql中字符串处理的几种处理方法concat、concat_ws、group_concat
    <经验杂谈>C#使用AES加密解密的简单介绍
    <经验杂谈>C#对CA证书加密解密的简单介绍
    C#实现HttpUtility.UrlEncode输出大写字母
    <微信应用开发系列>定时刷新AccessToken
    <经验杂谈>C#/.Net字符串操作方法小结
    <经验杂谈>查询表结构的SQL语句
    如何在.Net中使用Redis
    ASP.NET MVC进阶之路:深入理解Controller激活机制并使用Ioc容器创建对象
  • 原文地址:https://www.cnblogs.com/juexin/p/4058957.html
Copyright © 2011-2022 走看看