zoukankan      html  css  js  c++  java
  • 随机生成位置的可移动div

    用处不知道会有多大,做出来先备着(需引JQ)。div移动借鉴过园内某大大代码,就此感谢。

    在线demo:http://jsfiddle.net/seazeg/n3HGT/embedded/result/

    HTML:

    <body>
        <div style=" 100%; height: 100%; margin: 0 auto;" id="main">
            <div id="one">
                <img src="img2/1.jpg" />
            </div>
            <div id="two">
                <img src="img2/2.jpg" />
            </div>
            <div id="three">
                <img src="img2/3.jpg" />
            </div>
            <div id="four">
                <img src="img2/4.jpg" />
            </div>
            <div id="five">
                <img src="img2/1.jpg" />
            </div>
            <div id="six">
                <img src="img2/2.jpg" />
            </div>
            <div id="seven">
                <img src="img2/3.jpg" />
            </div>
            <div id="eight">
                <img src="img2/4.jpg" />
            </div>
            <input id="btn" type="button" value="打乱" style="float: left; margin: 210px 0 0 100px;
                z-index: 200; position: absolute;  100px; height: 50px;" />
            <input id="btn2" type="button" value="整理" style="float: left; margin: 210px 0 0 0px;
                z-index: 200; position: absolute;  100px; height: 50px;" />
        </div>
    </body>

    css:

     <style type="text/css">
            body
            {
                margin: 0;
                padding: 0;
            }
             #main div
            {
                height: 200px;
                width: 200px;
                border: 2px solid #999;
                position: absolute;
            }
            .pointer
            {
                cursor: pointer;
            }
            .on
            {
                box-shadow: 0 0 10px #111;
                cursor: move;
            }
            .off
            {
                box-shadow: 0 0 0 0;
                cursor: pointer;
            }
        </style>

    JS代码:

    <script type="text/javascript">
        $(function () {
            $("#btn").click(function () {//无序
                var len = $("#main div").length;
                for (var i = 0; i < len; i++) {
                    $("#main div").eq(i).css("top", Math.random() * 75 + "%");
                    $("#main div").eq(i).css("left", Math.random() * 80 + "%");
                }
            });
    
            $("#btn2").click(function () {//有序
                var len = $("#main div").length;
                for (var i = 0; i < len; i++) {
                    $("#main div").eq(i).css("top", "0");
                    $("#main div").eq(i).css("left", "0");
                }
            });
    
            $("#main div").mouseover(function () {
                var divid = $(this).attr("id"); //获取当前div的id
                var odiv = document.getElementById(divid); //得到当前div
                $(this).css("z-index", "100").siblings().css("z-index", "0"); //div层叠顺序
                $(this).addClass("pointer"); //初始化指针样式
                moveDiv(odiv);
            });
    
            function moveDiv(obj) {
                obj.onmousedown = function (e) {//鼠标按下事件
                    var oe = e || window.event;
                    var $this = this;
                    var startX = oe.clientX - $this.offsetLeft;
                    var startY = oe.clientY - $this.offsetTop;
                    obj.className = "on"; //css3阴影样式添加
                    document.onmousemove = function (e) {//鼠标移动事件
                        var oe = e || window.event;
                        $this.style.left = oe.clientX - startX + "px";
                        $this.style.top = oe.clientY - startY + "px";
                    }
    
                    document.onmouseup = function () {//鼠标松开事件
                        document.onmousemove = null;
                        document.onmouseup = null;
                        obj.className = "off"; //css3阴影样式去除
    
                        if ($this.releaseCapture) {//debug释放鼠标捕获
                            $this.releaseCapture();
                        }
                    }
                    if ($this.setCapture) {//debug设置鼠标捕获
                        $this.setCapture();
    
                    }
                    return false;
                }
            }
        });
    </script>
  • 相关阅读:
    opencv 图片像素x,y 访问!!!
    python numpy 三维数组 排序问题
    python+opencv水表识别
    tesseractOCR安装
    cookie 的寻找和使用以及页面滚动(python+selenium)
    你还在用a标签吗?——用button替代a
    js正则高级函数(replace,matchAll用法),实现正则替换(实测很有效)
    腾讯云服务器centos7.2+nginx(开启gzip压缩)+uwsgi+Django+react
    轮播图采用js、jquery实现无缝滚动和非无缝滚动的四种案例实现,兼容ie低版本浏览器
    webstorm 添加css前缀(兼容)自动添加
  • 原文地址:https://www.cnblogs.com/seazeg/p/2668257.html
Copyright © 2011-2022 走看看