博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript实现的拼图
阅读量:6255 次
发布时间:2019-06-22

本文共 2357 字,大约阅读时间需要 7 分钟。

  hot3.png

<!doctype html>

<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
         body{
            margin: 0;
            padding: 0;
        }
        #main{
            position: absolute;
            width: 600px;
            height: 600px;
            background-color: #999;
            border:1px solid #ddd;
            margin: 40px auto;
        }
        #main>div{
/*            position: absolute;*/
            width: 200px;
            height: 200px;
            float: left;
/*            cursor:pointer;*/
        }
        #main div:nth-child(1){
            position: absolute;
            background-image: url(1.png)
        }
        #main div:nth-child(2){
            position: absolute;
            left: 200px;
            background-image: url(9.png)
        }
        #main div:nth-child(3){
            position: absolute;
            left: 400px;
            background-image: url(7.png)
        }
        #main div:nth-child(4){
            position: absolute;
            top: 200px;
            background-image: url(8.png)
        }
        #main div:nth-child(5){
            position: absolute;
            left: 200px;
            top: 200px;
            background-image: url(2.png)
        }
        #main div:nth-child(6){
            position: absolute;
            left: 400px;
            top: 200px;
            background-image: url(3.png)
        }
       #main div:nth-child(7){
            position: absolute;
            top:400px;
            background-image: url(4.png)
        }
        #main div:nth-child(8){
            position: absolute;
            left: 200px;
            top: 400px;
            background-image: url(5.png)
        }
        #main div:nth-child(9){
            position: absolute;
            left: 400px;
            top: 400px;
            background-image: url(6.png)
        }
        
    </style>
</head>
<body>
    <div id="main">
        <div></div
        ><div></div
        ><div></div
        ><div></div
        ><div></div
        ><div></div
        ><div></div
        ><div></div
        ><div></div>
    </div>
    
    <script type="text/javascript">
        function $(id){
            return document.getElementById(id);
        }
        var boot =$('main').getElementsByTagName('div');
        
        var imgstatic = 0;
        for( i=0;i<boot.length;i++){
            boot[i].onmousedown = function(){
                imgstatic++;
                var oevent = window.event || arguments[0];
                var divnode= oevent.srcElement || oevent.target;
                divnode.style.zIndex="999";
            }
            boot[i].onmousemove = function(){
              var oevent = window.event || arguments[0];
              //  获得  img  对象
              var imgnode = oevent.srcElement || oevent.target;
                if(imgstatic ==1){
                    var mousex = oevent.clientX;
                    var mousey = oevent.clientY;
                    console.log(mousey);
                    var mouseoffx = oevent.offsetX;
                    var mouseoffy = oevent.offsetY;
                    imgnode.style.top = mousey-100+'px';
                    imgnode.style.left = mousex-100+'px';
                    
                }
           }
           boot[i].onmouseup = function(){
                imgstatic =0;
                var oevent = window.event || arguments[0];
                var divnode= oevent.srcElement || oevent.target;
                divnode.style.zIndex="0";
                
           }
             boot[i].onmouseout = function(){
                imgstatic =0;
            }
        }
    </script>
</body>
</html>

转载于:https://my.oschina.net/530520/blog/296076

你可能感兴趣的文章
unix ls命令
查看>>
Ajax核心技术之XMLHttpRequest
查看>>
使用T4模板生成不同部署环境下的配置文件
查看>>
如何把Json格式字符写进text文件中
查看>>
Linux: xclip,pbcopy,xsel用法 terminal 复制粘帖 (mac , ubuntu)
查看>>
[SVN(Ubuntu)] SVN 查看历史详细信息
查看>>
技术出身能做好管理吗?——能!
查看>>
抽象工厂模式
查看>>
如何折叠一段代码使整个代码看起来简洁
查看>>
Quartz2D绘制路径
查看>>
Java知多少(30)多态和动态绑定
查看>>
JDBC操作数据库
查看>>
Android中RelativeLayout的字符水平(垂直居中)对齐
查看>>
--@angularJS--独立作用域scope绑定策略之&符策略
查看>>
乾坤合一~Linux设备驱动之USB主机和设备驱动
查看>>
Python IDLE快捷键【转载合集】
查看>>
Bootstrap中glyphicons-halflings-regular.woff字体报404错notfound
查看>>
[C++] string与int, float, double相互转换
查看>>
ubuntu14.04安装chrome
查看>>
oracle中查询含字母的数据[正则表达式]
查看>>