Using CSS zoom to enhance JQuery/Javascript Smart Guides

Lately, I've been exploring the idea of incorporating 'smart guides' into one of my JQuery Draggable projects. This project involves a variable zoom level, and I've made adjustments in my code to accommodate it. However, I'm encountering difficulties getting this code to work smoothly with my smart guide code.

Everything functions as intended when using zoom:1, but when I set it to zoom:0.7, for instance, the divs start jittering around during drag and the guides sometimes fail to display.

I would greatly appreciate any assistance!

All code was originally sourced from Stack Overflow

JSFiddle Link: http://jsfiddle.net/adminb/zZ22h/

HTML

<div id="parent">
    <div class="other-objects" style="left:0px;top:300px;background:#a00;"></div>
    <div class="other-objects"></div>
    <div class="other-objects" style="left:400px;top:20px;"></div>
    <div class="objectx"></div>
    <div class="objecty"></div>
</div>

CSS

#parent {
    width:400px;
    height:400px;
    border:1px solid #000;
    position:absolute;
    zoom:0.8;
}
.other-objects {
    background:#aaa;
    width:100px;
    height:100px;
    display:block;
    position:relative;
    left:140px;
    top:50px;
}
.objectx {
    display:none;
    //background:#fff;
    width:0px;
    height:100%;
    position:absolute;
    top:0px;
    left:10px;
    border-left: 1px solid yellow;
}
.objecty {
    display:none;
    //background:#fff;
    width:100%;
    height:0px;
    position:absolute;
    top:10px;
    left:0px;
    border-bottom: 1px solid yellow;
}

JQuery / JavaScript

$.ui.plugin.add("draggable", "smartguides", {
    start: function (event, ui) {
        var i = $(this).data("draggable"),
            o = i.options;
        i.elements = [];
        $(o.smartguides.constructor != String ? (o.smartguides.items || ':data(draggable)') : o.smartguides).each(function () {
            var $t = $(this);
            var $o = $t.offset();
            if (this != i.element[0]) i.elements.push({
                item: this,
                width: $t.outerWidth(),
                height: $t.outerHeight(),
                top: $o.top,
                left: $o.left
            });
        });
    },
    stop: function (event, ui) {
        $(".objectx").css({
            "display": "none"
        });
        $(".objecty").css({
            "display": "none"
        });
    },
    drag: function (event, ui) {
        ui.position.top = Math.round(ui.position.top / zoom); //Compensate for zoom
        ui.position.left = Math.round(ui.position.left / zoom); //Compensate for zoom
        var inst = $(this).data("draggable"),
            o = inst.options;
        var d = o.tolerance;
        $(".objectx").css({
            "display": "none"
        });
        $(".objecty").css({
            "display": "none"
        });
        var x1 = ui.offset.left,
            x2 = x1 + inst.helperProportions.width,
            y1 = ui.offset.top,
            y2 = y1 + inst.helperProportions.height,
            xc = (x1 + x2) / 2,
            yc = (y1 + y2) / 2;
        for (var i = inst.elements.length - 1; i >= 0; i--) {
            var l = inst.elements[i].left,
                r = l + inst.elements[i].width,
                t = inst.elements[i].top,
                b = t + inst.elements[i].height,
                hc = (l + r) / 2,
                vc = (t + b) / 2;
            var ls = Math.abs(l - x2) <= d;
            var rs = Math.abs(r - x1) <= d;
            var ts = Math.abs(t - y2) <= d;
            var bs = Math.abs(b - y1) <= d;
            var hs = Math.abs(hc - xc) <= d;
            var vs = Math.abs(vc - yc) <= d;
            if (ls) {
                ui.position.left = inst._convertPositionTo("relative", {
                    top: 0,
                    left: l - inst.helperProportions.width
                }).left - inst.margins.left;
                $(".objectx").css({
                    "left": l - d - 4,
                        "display": "block"
                });
            }
            if (rs) {
                ui.position.left = inst._convertPositionTo("relative", {
                    top: 0,
                    left: r
                }).left - inst.margins.left;
                $(".objectx").css({
                    "left": r - d - 4,
                        "display": "block"
                });
            }

            if (ts) {
                ui.position.top = inst._convertPositionTo("relative", {
                    top: t - inst.helperProportions.height,
                    left: 0
                }).top - inst.margins.top;
                $(".objecty").css({
                    "top": t - d - 4,
                        "display": "block"
                });
            }
            if (bs) {
                ui.position.top = inst._convertPositionTo("relative", {
                    top: b,
                    left: 0
                }).top - inst.margins.top;
                $(".objecty").css({
                    "top": b - d - 4,
                        "display": "block"
                });
            }
            if (hs) {
                ui.position.left = inst._convertPositionTo("relative", {
                    top: 0,
                    left: hc - inst.helperProportions.width / 2
                }).left - inst.margins.left;
                $(".objectx").css({
                    "left": hc - d - 4,
                        "display": "block"
                });
            }
            if (vs) {
                ui.position.top = inst._convertPositionTo("relative", {
                    top: vc - inst.helperProportions.height / 2,
                    left: 0
                }).top - inst.margins.top;
                $(".objecty").css({
                    "top": vc - d - 4,
                        "display": "block"
                });
            }
        };
    }
});
$('.other-objects').draggable({
    containment: 'parent',
    smartguides: ".other-objects",
    tolerance: 5
});
var zoom = $('#parent').css('zoom');

Answer №1

Using the css property zoom can lead to various issues as it is non-standard and browser-dependent, with compatibility problems on Firefox and Opera.

Instead of using zoom, consider utilizing the css3 transform scale method: (don't forget to remove zoom and adjust the #parent margin after applying the scale)

-ms-transform: scale(0.7, 0.7); /* IE 9 */
-webkit-transform: scale(0.7, 0.7); /* Chrome, Safari, Opera */
transform: scale(0.7, 0.7);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

VueJS does not support loading Jquery

When setting up my project with Bootstrap, I encountered an issue where even though I imported the jquery.min file before Bootstrap in my main.js file using require(), Bootstrap still threw an error saying that it requires jQuery. In my main.js file, I ha ...

Utilize Bootstrap 4 to create a full screen desktop layout with columns that automatically adjust to

I am struggling to create a full-screen layout using Bootstrap 4, similar to the one shown in this image. I'm facing issues with the left and right columns not taking up the remaining space between the navigation and footer sections. Additionally, I n ...

Running Selenium tests are not able to initiate Javascript ajax requests

I'm currently troubleshooting a scenario using Selenium that involves the following steps: - When a user inputs text into a textarea, an ajax request is sent, which then adds the text to the database (implemented using django and processed in view.py) ...

Arranging Pictures Beside Text Using CSS

I have a puzzling issue that I cannot seem to find an answer for despite the countless times it has been asked. In my footer, there is aligned copyright text, and I am attempting to add 3 logos to the right of it without any line breaks. However, when the ...

Error occurs when attempting to execute JSON.parse within the backend of a Wordpress

My goal is to have a dropdown list of WordPress pages with the class of 'page_id' that can be repeated and added dynamically. Whenever a page is selected from the dropdown list, I want to send an AJAX request to fetch the title, excerpt, and thum ...

JavaScript, AJAX rapid iteration

I am working with some ajax code: $(document).ready( function() { $("#button1").click( function() { $.ajax({ type: "POST", url: "update.php", }); }); }); In my HTML code, I have 200 buttons. How can I ...

What factors influence the speed of an Ajax request?

It is common knowledge that the amount of data transmitted to the server, as well as the volume of information returned in a call can greatly affect its speed. However, what I am curious about is whether the following factors might also impact the transmi ...

Personalize the appearance of BeautifulSoup's prettify function based on the specified tag

Is there a way to customize the prettify function to prevent it from adding new lines to specific tags? I want to keep span and a tags from splitting up, like this: doc="""<div><div><span>a</span><span>b</sp ...

Creating an Art Deco inspired border using CSS

I am interested in creating a border effect using only CSS. Here is an image of the effect I am looking to achieve: https://i.sstatic.net/cKWXZm.jpg I would like to achieve this effect without adding additional div elements. Any suggestions or tips on how ...

Struggling with this CSS is really getting to me

I am struggling to create a modal with the tools and close button appearing on the same line. It should look similar to the image below: var modal = document.getElementById('myModal'); var btn = document.getElementById("myBtn"); var span = doc ...

Issue encountered while trying to modify the color of a placeholder

Whenever I attempt to modify the placeholder's color: ::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color: #909; } :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #909; opacity: 1; } : ...

When transmitting a base64 image to the server using an AJAX POST request, the forward slashes ("/") are being replaced by ampersands ("%")

I need to send an image as base64 to the server. This is how I am making the request: $.post(Config.serviceURL('captureImage/' + memid), { imageString: imageData }, function(data) { alert("Image uploaded!", data); }).fail(function() { ...

Creating stunning visuals with the power of SVG

I need to create a graphics editor similar to Paint using SVG for a school project. I have JavaScript code for drawing shapes like circles and lines, but when I try to add them to an onClick function for a button, it doesn't seem to be working. funct ...

Creating a webpage with a left panel and draggable elements using JavaScript/jQuery

As someone new to Javascript/jQuery, I have been given the task of creating a web page with elements in a "pane" on the left side that can be dragged into a "droppable" div area on the right side. The entire right side will act as a droppable zone. I am u ...

The issue of generating PDFs in PHP/Ajax encountering a timeout

I have a script written in PHP/ajax/jquery that generates documents containing values from a database. However, I am facing an issue when attempting to generate a large number of PDFs and Word files in a short amount of time. For each element in the array ...

Struggling with the transition of a CSS navigation menu from hover to focus

I'm attempting to transition a "mega menu" from 'hover' to 'focus'. My goal is to have the dropdown menus appear "on click" and remain visible until another top-level selection is clicked. Ideally, I am looking for a CSS-only solut ...

Disable the toggling of the dropdown functionality in the bootstrap function

Recently, I made some modifications to a bootstrap navbar by transforming it into a toolbar and adjusting a dropup dropdown to include two datepicker elements. An issue arose when the dropdown would collapse upon selecting a date. To address this problem, ...

Centering an image that is absolutely positioned within a container that is relatively positioned, horizontally

Looking to center an image that is positioned absolutely horizontally within a container that is relatively positioned. After attempting with CSS and not finding success, I ended up using Jquery. http://jsfiddle.net/CY6TP/ [This is my attempt using Jquery ...

Having trouble with a broken link on your HTML email button?

I'm attempting to add a button to an HTML email that redirects to a link within an href tag. Admittedly, my knowledge of html code is minimal. Here's what I've attempted: <p> <input style="width: 200px; padding: 15px; box-shadow: ...

Displaying or concealing content with a jQuery toggle button

Currently, I am developing more/less buttons for text using jQuery. My goal is to initially display only a portion of the text in a div when the jQuery script is loaded, specifically hiding any text exceeding 650 characters. However, I am facing challenges ...