Jquery draggable with zoom problem

后端 未结 5 1238
面向向阳花
面向向阳花 2020-12-05 11:21

I am working on a page in witch all its contents are scaled by using zoom. The problem is that when I drag something in the page the dragging item gets a bad position that s

5条回答
  •  生来不讨喜
    2020-12-05 11:49

    If CSS Zoom is performed specifically on the html body, there is a very easy fix, but it does require you to modify the source. That's a no-go for some, but I'll post the solution here anyway.

    So the draggable functionality of jQuery UI figures out the mouseposition using a method called _generatePosition. The problem is that it doesn't take zoom into account (duh), so we merely have to divide its outcome by the zoom level and everything will work.

    So turn this:

        return {
            top: (
    
                // The absolute mouse position
                pageY -
    
                // Click offset (relative to the element)
                this.offset.click.top -
    
                // Only for relative positioned nodes: Relative offset from element to offset parent
                this.offset.relative.top -
    
                // The offsetParent's offset without borders (offset + border)
                this.offset.parent.top +
                ( this.cssPosition === "fixed" ?
                    -this.offset.scroll.top :
                    ( scrollIsRootNode ? 0 : this.offset.scroll.top ) )
            )/ ,
            left: (
    
                // The absolute mouse position
                pageX -
    
                // Click offset (relative to the element)
                this.offset.click.left -
    
                // Only for relative positioned nodes: Relative offset from element to offset parent
                this.offset.relative.left -
    
                // The offsetParent's offset without borders (offset + border)
                this.offset.parent.left +
                ( this.cssPosition === "fixed" ?
                    -this.offset.scroll.left :
                    ( scrollIsRootNode ? 0 : this.offset.scroll.left ) )
            )
        };
    

    in

        var zoomLevel = parseFloat($('body').css("zoom") || "1.0");
    
        return {
            top: (
    
                // The absolute mouse position
                pageY -
    
                // Click offset (relative to the element)
                this.offset.click.top -
    
                // Only for relative positioned nodes: Relative offset from element to offset parent
                this.offset.relative.top -
    
                // The offsetParent's offset without borders (offset + border)
                this.offset.parent.top +
                ( this.cssPosition === "fixed" ?
                    -this.offset.scroll.top :
                    ( scrollIsRootNode ? 0 : this.offset.scroll.top ) )
            ) / zoomLevel,
            left: (
    
                // The absolute mouse position
                pageX -
    
                // Click offset (relative to the element)
                this.offset.click.left -
    
                // Only for relative positioned nodes: Relative offset from element to offset parent
                this.offset.relative.left -
    
                // The offsetParent's offset without borders (offset + border)
                this.offset.parent.left +
                ( this.cssPosition === "fixed" ?
                    -this.offset.scroll.left :
                    ( scrollIsRootNode ? 0 : this.offset.scroll.left ) )
            ) / zoomLevel
        };
    

    That's all. I suggest saving it under a specific name so it's easily recognized that it has been modified. E.g.: jquery-ui.ZOOMFIX.js

    I used the jquery-ui.js file from the following package: https://jqueryui.com/resources/download/jquery-ui-1.12.1.zip

提交回复
热议问题