码迷,mamicode.com
首页 > Web开发 > 详细

HTML5拖动

时间:2015-07-12 23:08:56      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

  <style>
        #a{
            height: 100px;
            width: 100px;
            border: 1px solid black;
            float: left;
            margin-left: 200px;
            text-align: center;
            line-height: 100px;
        }
        #b{
            height: 100px;
            width: 100px;
            line-height: 100px;
            border: 1px solid black;
            float: right;
            margin-right: 200px;
            text-align: center;
        }
    </style>
</head>
<body>
<div id="a"><img src="img/w3school_logo_black.gif" draggable="true"></div>
<div id="b"></div>

</body>
<script>
    (function(){

    })();
    function $() {
        var elements = new Array();

        for (var i = arguments.length - 1; i >= 0; i--) {
            var element = arguments[i];

            if (typeof element == "string") {
                element = document.getElementById(element);
            }

            if (arguments.length == 1) {
                return element;
            } else {
                elements.push(element);
            }

            return elements;
        };
    };


    //封装事件处理
    function addEvent(node, type, listener) {
        if (!(node = $(node))) return false;
        if (node.addEventListener) {
            //W3C
            node.addEventListener(type, listener, false);
            return true;
        } else if (node.attachEvent) {
            node[‘e‘ + type + listener] = listener;
            node[type + listener] = function() {
                node[‘e‘ + type + listener](window.event);
            };
            node.attachEvent(‘on‘ + type, node[type + listener]);
            return true;
        }

        return false;

    };

    //移出事件
    function removeEvent(node,type,listener){
        if (!(node = $(node))) return false;
        if(node.removeEventListener){
            node.removeEventListener(type,listener,false);
            return true;
        }else if(node.removeEvent){
            node.detachEvent(‘on‘+type,node[type+listener]);
            node[type+listener] = null;
            return true;
        }
        return false;
    };
    //获取事件对象
    function getEventObject(e){
        return e || window.event;

    }
    //阻止事件冒泡
    function stopPropagation(eventObject){
        var eventObject = eventObject || getEventObject();
        if(eventObject.stopPropagation){
            eventObject.stopPropagation();
        }else{
            eventObject.cancelBubble = true;
        }
    };
    //阻止浏览器默认 事件
    function stopDefault(eventObject){
        var eventObject = eventObject || getEventObject();
        if(eventObject.preventDefault){
            eventObject.preventDefault();
        }else{
            eventObject.returnValue = false;
        }
    };
    //获取鼠标点击时的x和y坐标
    function getPointerPositionInDocument(eventObject) {
        eventObject = eventObject || getEventObject(eventObject);
        var x = eventObject.pageX || (eventObject.clientX +
                (document.documentElement.scrollLeft || document.body.scrollLeft));
        var y = eventObject.pageY || (eventObject.clientY +
                (document.documentElement.scrollTop || document.body.scrollTop));
        return {
            ‘x‘: x,
            ‘y‘: y
        };
    };

    //获取文档的宽和高
    function getWindowSize(){
        var width = height = 0;
        if(this.innerWidth){
            width = this.innerWidth;
            height = this.innerHeight;
        }else if(document.documentElement && document.documentElement.clientHeight){
            width = document.documentElement.clientWidth;
            height = document.documentElement.clientHeight;
        }else if(document.body && document.body.clientHeight){
            width = document.body.clientWidth;
            height = document.body.clientHeight;
        }

        return {‘width‘:width,‘height‘:height};
    };

    //获取标签的宽、高和left、top
    function getDimensions(element){
        if (!(element = $(element))) return false;

        return {
            ‘left‘:element.offsetLeft,
            ‘top‘:element.offsetTop,
            ‘width‘:element.offsetWidth,
            ‘height‘:element.offsetHeight
        };
    }

    //设置标签样式
    function setStyle(element, styles) {
        if (!(element = $(element))) return false;
        for (property in styles) {
            if (!styles.hasOwnProperty(property)) continue;

            if (element.style.setProperty) {
                element.style.setProperty(
                        uncamelize(property, ‘-‘), styles[property], null);
            } else {
                element.style[camelize(property)] = styles[property];
            }
        }
        return true;
    };
    //将-形式的字符串转换为驼峰命名法的字符串
    function camelize(s) {
        return s.replace(/-(\w)/g, function(strMatch, p1) {
            return p1.toUpperCase();
        });
    };

</script>

HTML5拖动

标签:

原文地址:http://www.cnblogs.com/wangwei1234/p/4641608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!