完美拖拽及回放

2019-08-09

21:54:40

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title>完美拖拽</title>
    <style type="text/css">
        html,
        body {
            overflow: hidden;
        }

        body,
        div,
        h2,
        p {
            margin: 0;
            padding: 0;
        }

        body {
            color: #fff;
            background: #000;
            font: 12px/2 Arial;
        }

        p {
            padding: 0 10px;
            margin-top: 10px;
        }
        span {
            color: #ff0;
            padding-left: 5px;
        }
        #box {
            position: absolute;
            width: 300px;
            height: 150px;
            background: #333;
            border: 2px solid #ccc;
            top: 50%;
            left: 50%;
            margin: -75px 0 0 -150px;
        }

        #box h2 {
            height: 25px;
            cursor: move;
            background: #222;
            border-bottom: 2px solid #ccc;
            text-align: right;
            padding: 0 10px;
        }

        #box h2 a {
            color: #fff;
            font: 12px/25px Arial;
            text-decoration: none;
            outline: none;
        }
    </style>
</head>
<body>
    <div id="box" style="margin-left: 0px; margin-top: 0px; left: 533px; top: 231px;">
        <h2 id="tz"><a href="javascript:;" id="a1">点击回放拖动轨迹</a></h2>
        <p><strong>Drag:</strong><span>false</span></p>
        <p><strong>offsetTop:</strong><span>231</span></p>
        <p><strong>offsetLeft:</strong><span>533</span></p>
    </div>
</body></html>
<script src="../public.js"></script>
<script>

    var tz = $id("tz");
    var a1 = $id("a1");
    var box = $id("box");
    var p = document.getElementsByTagName("p")

    var positionArr = [];

    tz.onmousedown = function(eve){
        var e = eve || event;
        var x = e.offsetX;
        var y = e.offsetY;
        p[0].childNodes[1].innerHTML = "ture"
        document.onmousemove = function(eve){
            var e = eve || event;
            var l = e.clientX - x;
            var t = e.clientY - y;

            //保存轨迹
            positionArr.push({left:l,top:t});

            box.style.left = l + "px";
            box.style.top = t + "px";
            return false;
        }
        document.onmouseup = function(){
            this.onmousemove = null;
            console.log(positionArr);
            p[0].childNodes[1].innerHTML = "flase";
        }
    }

    a1.onclick = function(){
        //没有拖拽就不需要回放
        if(positionArr.length > 0){
            var index = 0;
            //根据拖拽时产生的坐标轨迹从后往前慢慢赋值
            var timer = setInterval(function(){
                index++;
                box.style.left = positionArr[positionArr.length-index].left + "px";
                box.style.top = positionArr[positionArr.length-index].top + "px";
                if(index == positionArr.length){
                    clearInterval(timer);
                    //清除原来的轨迹
                    positionArr.length = 0;
                }
            },10);
        }
    }

</script>

(有两个方案,不过这个更简洁,所以发这个给各位看官。今日心情不佳,如有需要源代码,可以给我留言)

原文地址:https://www.cnblogs.com/g-code/p/11329803.html

时间: 2024-10-28 11:47:30

完美拖拽及回放的相关文章

完美拖拽 &amp;&amp;仿腾讯微博效果&amp;&amp; 自定义多级右键菜单

<!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-

完美拖拽

html代码: <div id="div1"></div> css代码: #div1{width:100px;height:100px;background:red;position:absolute} javascript代码: window.onload=function(){     var oDiv=document.getElementById("div1");     var x=0;     var y=0;     oDiv.

完美拖拽思路

<!doctype html><html><head><meta charset="utf-8"><meta name="author" content=""><meta name="copyright" content="智"><title></title><style>* { margin:0;

js完美拖拽封装及其案例

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <title></title> <style type="text/css"> #bo

JS完美拖拽

<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } #box{ height: 100px; width: 100px; background:red; position: absolute; cu

拖拽回放

<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>完美拖拽</title> <style type="text/css"> html, body { overflow:hidden; } body, div, h

js实现回放拖拽轨迹-------Day48

今天有点小高兴,csdn博客浏览量过万了,在过去还从来没有过这么高的浏览量呢.不得不说.太多时候还是有些矫情.可看到这些鼓舞还是忍不住高兴啊,至少,这样让我有一种行内人员的感觉,吾道不孤啊. 闲话不多说.继续今天的记录,记录回放拖拽痕迹,先从过程上进行分析: 1.要实现回放拖拽痕迹,则必须先有记录: 2.要记录拖拽痕迹,则必需要实现拖拽. 这个问题前几天以前做到过.当时实现的也略有瑕疵,但大致的实现方法已经了然于胸.所以今天在实现这个问题的时候速度快了不少,着实高兴了一番,今天再实现了一遍之后理

鼠标拖拽回放

<html> <head> <title>拖拽回放</title> <meta charest="utf-8"> <style> div{width: 80px;height: 80px;background: red;position: absolute;} </style> <script> window.onload=function(){ var oInp=document.getE

android开发游记:SpringView 下拉刷新的高效解决方案,定制你自己风格的拖拽页面

关于下拉刷新/上拉加载更多的解决方案网上已经有很多了,浏览了目前主流的下拉控件比如PullToRefresh库等,第一:大多数实现库都难以进行动画和样式的自定义.第二:不能很好的兼容多种滚动控件,它们都对listView.RecyclerView等进行了不同程度的重新实现,你在项目中就得使用库提供的PullToRefreshListView.PullToRefreshRecyclerView等来代替源生的listView.RecyclerView等,这样的方式其实并不好,随着android版本的