我有一个WordPress gallery 网站,在那里图像在Bootstrap 5模式中打开,这打开了Bootstrap Carousel.

图像被加载到带有php Foreach循环的POST循环中,每个幻灯片一个.

我正在try 添加悬停放大到每个图像与一个小圆圈使用blowup.js.

我有每个幻灯片正确移动的zoom 功能,但是只有第一个图像跟踪鼠标所在的位置.随后的放大显示在左上角.

Here is my Function:

(function ($) {
    
    // Update blowup when slide changes
    $("#gallery-carousel").on('slide.bs.carousel', function (){
        $(".img-zoom").blowup("destroy");
    });

    $("#gallery-carousel").on('slid.bs.carousel', function (){
        $(".img-zoom").blowup()
    });
      
})(jQuery);
      

This is the code that the function targets within the Carousel:


<div class="carousel-item">

 <div class="position-relative carousel-img">
      <img class="img-fluid img-zoom" src="<?php echo $image_url; ?>" />                                
 </div>

</div>

This is the JQuery in the plugin that is meant to track the zoomed coordinates:

  // Mouse motion on image
    $element.mousemove(function (e) {

      // Lens position coordinates
      var lensX = e.pageX - $options.width / 2;
      var lensY = e.pageY - $options.height / 2;

      // Relative coordinates of image
      var relX = e.pageX - $(this).offset().left;
      var relY = e.pageY - $(this).offset().top;
     
      // Zoomed image coordinates 
      var zoomX = -Math.floor(relX / $element.width() * (NATIVE_IMG.width * $options.scale) - $options.width / 2);
      var zoomY = -Math.floor(relY / $element.height() * (NATIVE_IMG.height * $options.scale) - $options.height / 2);

      var backPos = zoomX + "px " + zoomY + "px";
      var backgroundSize = NATIVE_IMG.width * $options.scale + "px " + NATIVE_IMG.height * $options.scale + "px";

    })

推荐答案

关键是将"zoom 图像坐标"从"$(元素)"更改为"$(这)".

我还将所有‘var’更改为‘const’

以下是使其运行的最终代码.

     
/**
 * blowup.js
 * Paul Krishnamurthy 2016
 *
 * https://paulkr.com
 * paul@paulkr.com
 */

 (function ($) {
  $.fn.blowup = function (attributes) {

    const $element = this;

    // If the target element is not an image
    if (!$element.is("img")) {
      console.log("%c Blowup.js Error: " + "%cTarget element is not an image.",
        "background: #FCEBB6; color: #F07818; font-size: 17px; font-weight: bold;",
        "background: #FCEBB6; color: #F07818; font-size: 17px;");
      return;
    }

    // Default attributes
    const defaults = {
      round         : true,
      width         : 200,
      height        : 200,
      background    : "transparent",
      shadow        : "0 8px 17px 0 rgba(0, 0, 0, 0.2)",
      border        : "6px solid #FFF",
      cursor        : true,
      zIndex        : 999999,
      scale         : 1,
      customClasses : ""
    }

    // Update defaults with custom attributes
    const $options = $.extend(defaults, attributes);

    // Modify target image
    $element.on('dragstart', function (e) { e.preventDefault(); });
    $element.css("cursor", $options.cursor ? "crosshair" : "none");

    // Create magnification lens element
    const lens = document.createElement("div");
    lens.id = "BlowupLens";

    // Attach the element to the body
    $("body").append(lens);

    // Updates styles
    $blowupLens = $("#BlowupLens");

    $blowupLens.css({
      "position"          : "absolute",
      "display"           : "none",
      "pointer-events"    : "none",
      "zIndex"            : $options.zIndex,
      "width"             : $options.width,
      "height"            : $options.height,
      "border"            : $options.border,
      "background"        : $options.background,
      "border-radius"     : $options.round ? "50%" : "none",
      "box-shadow"        : $options.shadow,
      "background-repeat" : "no-repeat",
    });

    // Add custom CSS classes
    $blowupLens.addClass($options.customClasses);

    // Show magnification lens
    $element.mouseenter(function () {
      $blowupLens.css("display", "block");
    })

    // Mouse motion on image
    $element.mousemove(function (e) {

      // Constants
      const $IMAGE_URL    = $('.active').find('img').attr('src');
      const NATIVE_IMG    = new Image();
      NATIVE_IMG.src    = $('.active').find('img').attr('src');

      // Lens position coordinates
      const lensX = e.pageX - $options.width / 2;
      const lensY = e.pageY - $options.height / 2;

      // Relative coordinates of image
      const relX = e.pageX - $(this).offset().left;
      const relY = e.pageY - $(this).offset().top;

      // Zoomed image coordinates
      zoomX =-6 -Math.floor(relX / $(this).width() * (NATIVE_IMG.width * $options.scale) - $options.width / 2);
      zoomY =-6 -Math.floor(relY / $(this).height() * (NATIVE_IMG.height * $options.scale) - $options.height / 2);
  
      const backPos = zoomX + "px " + zoomY + "px";
      const backgroundSize = NATIVE_IMG.width * $options.scale + "px " + NATIVE_IMG.height * $options.scale + "px";

      // Apply styles to lens
      $blowupLens.css({
        left                  : lensX,
        top                   : lensY,
        "background-image"    : "url(" + encodeURI($IMAGE_URL) + ")",
        "background-size"     : backgroundSize,
        "background-position" : backPos
      });
    })

    // Hide magnification lens
    $element.mouseleave(function () {
      $blowupLens.css("display", "none");
    });
  }
    })(jQuery);
    


// Artwork Magnification 

(function ($) {

    $(".img-zoom").blowup()

})(jQuery);

Php相关问答推荐

邮箱打开跟踪器不工作时发送邮件使用laravel调度程序

dockerized laravel服务器拒绝POST请求

make dd()如何正确工作?¨

在不超过PHP时间限制的情况下,在Laravel中高效地插入批量数据(25万条记录)?(队列、多租户)

Php解压缩deflate64

MULTI UPDATE Laravel为什么只能更新我的第一行

如何确保所有语言文件都使用Laravel中的新密钥进行更新?

列出所有WooCommerce处理订单中的产品数量,SKU和品牌

如何在Livewire 3 Form类中做依赖注入?

如何从Laravel中的Spatie\Dns\Records\A对象中提取IP地址

首先在WooCommerce我的帐户订单页面中移动操作按钮

Regex|PHP捕获json字符串中的每个非法双引号

有没有办法获取触发流操作的页面的 URI?

如何显示所有数组数据而不仅仅是最后的结果

使用动态地址和动态文件创建zip文件并获取zip文件链接

在 mysql 上的 PDO 中启用自动提交并将自动提交设置为关闭

Laravel 路由参数中的 ":" 是什么?

PHP 中的正则表达式,具有字符范围,但不包括特定字符

如何将子查询结果添加到学说 2 中主查询的末尾?

将加密/解密函数从 PHP 转换为 NodeJS