zoukankan      html  css  js  c++  java
  • 用js实现多个效果一起变化

    效果如下,有时可能会多个效果一起变化。

     

    要点一:

    box.onmouseover = function(){
    startrun(box,{200,height:300},function(){
    startrun(box,{opacity:100})
    })
    }

    通过json串可以传很多值,这样就可以同时改变很多属性了。

    要点二:

    遍历json串中的属性值对,找出所有的属性和对应的值,计算相应的数。

    并设置一个标识位,当没达到目标值时,标识位设为false,当标识位为true时,可以关闭定时器,执行下一次的调用

    最后,上代码

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="gb2312" />
    <title>无标题文档</title>
    <style>
    <!--
    body
    {margin:0; padding:0; font:12px/1.5 arial;}
    #box
    {width:100px; height:100px; position:absolute; background:#06c; filter:alpha(opacity=30); opacity:0.3}
    -->
    </style>
    <script>
    <!--
    window.onload
    = function(){
    var box = document.getElementById("box");
    box.onmouseover
    = function(){
    startrun(box,{
    200,height:300},function(){
    startrun(box,{opacity:
    100})
    })
    }
    box.onmouseout
    = function(){
    startrun(box,{
    100,height:100},function(){
    startrun(box,{opacity:
    30})
    })
    }
    }

    function getstyle(obj,name){
    if(obj.currentStyle){
    return obj.currentStyle[name];
    }
    else{
    return getComputedStyle(obj,false)[name];
    }
    }

    function startrun(obj,json,fn){
    clearInterval(obj.timer);
    obj.timer
    = setInterval(function(){
    var isall = true;
    for(var attr in json){
    var cur=0;
    if(attr == "opacity"){
    cur
    = Math.round(parseFloat(getstyle(obj,attr))*100);
    }
    else{
    cur
    = parseInt(getstyle(obj,attr));
    }

    var speed = (json[attr] - cur)/8
    speed = speed>0?Math.ceil(speed):Math.floor(speed);
    if(cur != json[attr]){
    isall
    = false;
    }
    if(attr == "opacity"){
    obj.style.filter
    = "alpha(opacity="+(cur+speed)+")";
    obj.style.opacity
    = (cur+speed)/100;
    }else{
    obj.style[attr]
    = cur+speed+"px";
    }
    }

    if(isall){
    clearInterval(obj.timer);
    if(fn){
    fn();
    }
    }

    },
    30);
    }
    //-->
    </script>
    </head>

    <body>
    <div id="box">
    </div>
    </body>
    </html>



  • 相关阅读:
    firewalld添加/删除服务service,端口port
    centos7下配置ftp服务器
    CentOS安装vsftpd FTP后修改默认21端口方法
    虚拟机,安装tools时出现“安装程序无法继续解决
    Linux下mysql数据库备份
    测试linux下磁盘的读写速率
    redis状态详解
    office2010安装不了提示已经安装32位的了怎么办
    nginx安装部署
    结构体赋值
  • 原文地址:https://www.cnblogs.com/jingangel/p/2396166.html
Copyright © 2011-2022 走看看