求大神给个比较好的页面滑动效果,滑动是用onmouseup事件和onmousedown事件触发的,在网上找了很多感觉都不理想,希望哪位大神 帮帮忙了

解决方案 »

  1.   

    <body>
    <div id="div" style='height:100px;width:100px;border:1px solid #000;position:absolute'></div>
    <script type="text/javascript">
    function bind()
    {
        if (!Function.prototype.bind) 
        {
            Function.prototype.bind = function(obj) 
            {
                var owner = this,
                args = Array.prototype.slice.call(arguments),
                callobj = Array.prototype.shift.call(args);
                return function(e) 
                {
                    e = e || top.window.event || window.event;
                    owner.apply(callobj, args.concat([e]));
                };
            };
        }
    }
    var drag = function()
    {
        var startX , startY , objX , objY , moveX , moveY;
        var parent = this;
        bind()
        var down = function(e)
        {
            e = e || window.event;
            startX = e.clientX;
            startY = e.clientY;
            objX = parent.offsetLeft;
            objY = parent.offsetTop;
            moveX = startX - objX;
            moveY = startY - objY;
            if(!document.addEventListener) this.setCapture();
        
            document.onmousemove = move;
            document.onmouseup = up.bind(this)
            
            if(e.stopPropagation) e.stopPropagation() 
                else e.cancelBubble = true
            if(e.preventDefault) e.preventDefault() 
                else e.returnValue = false            
        }
        
        var move = function(e)
        {
             e = e || window.event;
             startX = e.clientX;
             startY = e.clientY;
             parent.style.left = startX - moveX + "px";
             parent.style.top = startY - moveY + "px"; 
             
            if(e.stopPropagation) e.stopPropagation() 
                else e.cancelBubble = true
            if(e.preventDefault) e.preventDefault() 
                else e.returnValue = false             
        }
        
        var up = function(e)
        {
            document.onmousemove = null;
            if(!document.addEventListener) this.releaseCapture();
            
            if(e.stopPropagation) e.stopPropagation() 
                else e.cancelBubble = true
            if(e.preventDefault) e.preventDefault() 
                else e.returnValue = false    
        }
        
        this.onmousedown = down;
    }this.drag.call(document.getElementById("div"))
    </script>
    </body>
    是否这种?