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 type="text/javascript">
    var minheight = 20;
    var maxheight = 300;
    var time = 500;
    var timer = null;
    var toggled = false;

    window.onload = function () {
    var controler = document.getElementById('slide');
    var slider = document.getElementById('slider');
    slider.style.height = minheight + 'px';
    controler.onclick = function () {
    clearInterval(timer);
    var instanceheight = parseInt(slider.style.height);
    var init = (new Date()).getTime();
    var height = (toggled = !toggled) ? maxheight : minheight;

    var disp = height - parseInt(slider.style.height);
    timer = setInterval(function () {
    var instance = (new Date()).getTime() - init;
    if (instance < time) {
    var pos = Math.floor(disp * instance / time);
    result = instanceheight + pos;
    slider.style.height = result + 'px';
    document.getElementById('log').innerHTML = 'Current Height : <b>' + result + '</b><br /> Current Time : <b>' + instance + '</b>';
    } else {
    slider.style.height = height + 'px'; //safety side ^^
    clearInterval(timer);
    controler.value = toggled ? ' Slide Up ' : ' Slide Down ';
    document.getElementById('log').innerHTML = 'Current Height : <b>' + height + '</b><br /> Current Time : <b>' + time + '</b>';
    }
    }, 1);
    };
    };
    </script>
    </head>
    <body>
    <h1>
    test
    </h1>
    <input type="button" id="slide" value=" Slide Down " />
    <span id="log" style="position: absolute; left: 10px; top: 150px;"></span>
    <br />
    <div id="slider" style="background-color: green;">
    content goes here
    </div>
    <div style="height: 20px; 100%; background-color: Gray;">
    </div>
    </body>
    </html>

  • 相关阅读:
    JS 中 new 操作符
    js清除浏览器缓存的几种方法
    一个自定义分享按钮
    解决windows下nginx中文文件名乱码
    sublime text 3 添加 javascript 代码片段 ( snippet )
    transition动画最简使用方式
    hammerjs jquery的选项使用方法,以给swipe设置threshold和velocity为例
    sublime text 3 的emmet 添加自定义 html 片段
    解决 placeholder 垂直不居中,偏上的问题
    Sublime Text 3 配置 sass
  • 原文地址:https://www.cnblogs.com/handsomer/p/4527187.html
Copyright © 2011-2022 走看看