jQuery 不更新元素的 CSS 高度和宽度

发布于 2024-12-17 19:25:04 字数 2292 浏览 4 评论 0原文

我在让该应用程序的功能在 Wordpress 中 100% 运行时遇到了很多麻烦。我在 Wordpress 之外的服务器上有一个该应用程序的工作版本,但是当涉及到 Wordpress 时,事情就会变得很奇怪。

我现在遇到的问题是该过程的第二步,用户可以裁剪图像的一部分以显示在二维码的中心。 在这里您可以看到工作示例以及应该发生的情况,以及此处 您可以看到它在第二步中中断的位置。我猜测 WordPress 主题中的某个地方存在 CSS 冲突,因为 jQuery 似乎工作正常。 Inspect 元素显示在工作示例中,边距和高度/宽度正在通过裁剪后的选择进行动态调整,但在损坏的示例中,高度/宽度根本没有调整。我尝试禁用主题上的所有 CSS 文件,但无济于事。

这是我们用来在左侧图像被裁剪时更新右侧图像的 jQuery。我们使用的插件是jcrop。问题是,在工作版本上,高度和宽度会使用内联 css 正确更新,但在损坏的版本上,这些值不会,但是边距在两个版本上都可以正常工作。

//function to update preview divs
jQuery(function($){
    var jcrop_api, boundx, boundy; //set jcrop variables

    function updatePreview(c)
    {
        if (parseInt(c.w) > 0)
        {
            var rx = 73 / c.w;
            var ry = 73 / c.h;

            jQuery('#preview').css({
                width: Math.round(rx * boundx) + 'px !important',
                height: Math.round(ry * boundy) + 'px !important',
                marginLeft: '-' + Math.round(rx * c.x) + 'px !important',
                marginTop: '-' + Math.round(ry * c.y) + 'px !important'
            });
        }
    };

    //function to update coordinates
    function updateCoords(c)
    {
        jQuery('#x').val(c.x);
        jQuery('#y').val(c.y);
        jQuery('#w').val(c.w);
        jQuery('#h').val(c.h);
    };

    jQuery(window).load(function () {
        var PathToFile = jQuery('#cropImageDisplay').attr("name");
        jQuery('#cropImageDisplay').load("/wp-content/themes/howfarqr/resources/php/uploadedImage.php?fn="+PathToFile).hide().fadeIn('slow', function() {
            jQuery('#cropbox').Jcrop({ //jcrop selector
                onChange: updatePreview, //function to execute onChange
                onSelect: updateCoords, //function to execute onSelect
                aspectRatio: 1 //asepct ratio
            },function(){ //callback function
                    var bounds = this.getBounds(); // get the real image size
                boundx = bounds[0]; //assign x
                boundy = bounds[1]; //assign y
                //store jcrop api as jcrop variable
                jcrop_api = this;
            });
        });
    });
});  

I've had a lot of trouble getting the functionality of this application to work 100% in Wordpress. I have a working version of the application on the server outside of Wordpress, however when Wordpress gets involved things get funky.

The issue that I am having right now is on the second step of the process, when a user can crop a portion of the image to be displayed in the center of the qr code. Here you can see the working example and what should happen, and here you can see where it breaks on the second step. I'm guessing there is a CSS conflict somewhere in the Wordpress theme, since jQuery seems to be working alright. Inspect element shows that on the working example, the margins and height/width are being adjusted on the fly with the cropped selection, but on the broken example the height/width aren't being adjusted at all. I've tried disabling all of the CSS files that are on the theme, but to no avail.

Here is the jQuery we are using to update the image on the right as the image on the left is cropped. The plugin we are using is jcrop. The issue is that on the working version, the height and width are updated correctly with inline css, but on the broken version these values are not, however the margins are working correctly on both versions.

//function to update preview divs
jQuery(function($){
    var jcrop_api, boundx, boundy; //set jcrop variables

    function updatePreview(c)
    {
        if (parseInt(c.w) > 0)
        {
            var rx = 73 / c.w;
            var ry = 73 / c.h;

            jQuery('#preview').css({
                width: Math.round(rx * boundx) + 'px !important',
                height: Math.round(ry * boundy) + 'px !important',
                marginLeft: '-' + Math.round(rx * c.x) + 'px !important',
                marginTop: '-' + Math.round(ry * c.y) + 'px !important'
            });
        }
    };

    //function to update coordinates
    function updateCoords(c)
    {
        jQuery('#x').val(c.x);
        jQuery('#y').val(c.y);
        jQuery('#w').val(c.w);
        jQuery('#h').val(c.h);
    };

    jQuery(window).load(function () {
        var PathToFile = jQuery('#cropImageDisplay').attr("name");
        jQuery('#cropImageDisplay').load("/wp-content/themes/howfarqr/resources/php/uploadedImage.php?fn="+PathToFile).hide().fadeIn('slow', function() {
            jQuery('#cropbox').Jcrop({ //jcrop selector
                onChange: updatePreview, //function to execute onChange
                onSelect: updateCoords, //function to execute onSelect
                aspectRatio: 1 //asepct ratio
            },function(){ //callback function
                    var bounds = this.getBounds(); // get the real image size
                boundx = bounds[0]; //assign x
                boundy = bounds[1]; //assign y
                //store jcrop api as jcrop variable
                jcrop_api = this;
            });
        });
    });
});  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2024-12-24 19:25:04

该问题与 boundxboundy 未定义有关。查看传递给 .css() 的对象(使用断点):

> console.log({
    width: Math.round(rx * boundx) + 'px',
    height: Math.round(ry * boundy) + 'px',
    marginLeft: '-' + Math.round(rx * c.x) + 'px',
    marginTop: '-' + Math.round(ry * c.y) + 'px'
})
▼ Object
    height: "NaNpx"
    marginLeft: "-25px"
    marginTop: "-9px"
    width: "NaNpx"
    __proto__: Object
> boundx
undefined

了解现在的原因。


啊哈:

在此处输入图像描述

两个页面上的 JavaScript 不相同!


现在看起来 Jcrop 回调函数根本没有被调用。不知道为什么。


这两个页面也使用不同版本的 Jcrop。 工作实现版本为 v0.9.9,并且非 -工作是使用 似乎是 0.9.8

The problem is related to the fact that boundx and boundy are not defined. Looking at the object that's passed to .css() (using a breakpoint):

> console.log({
    width: Math.round(rx * boundx) + 'px',
    height: Math.round(ry * boundy) + 'px',
    marginLeft: '-' + Math.round(rx * c.x) + 'px',
    marginTop: '-' + Math.round(ry * c.y) + 'px'
})
▼ Object
    height: "NaNpx"
    marginLeft: "-25px"
    marginTop: "-9px"
    width: "NaNpx"
    __proto__: Object
> boundx
undefined

Looking into why that is now.


Aha:

enter image description here

The JavaScript on the two pages is not identical!


Now it looks like the Jcrop callback function isn't called at all. Not sure why.


The two pages are also using different versions of Jcrop. The working implementation has v0.9.9, and the not-working is using what appears to be 0.9.8.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文