【问题标题】:Integrate JQuery Image Magnification with Dynamic Bootstrap 5 Carousel将 JQuery 图像放大与 Dynamic Bootstrap 5 Carousel 集成
【发布时间】:2022-11-19 14:50:54
【问题描述】:

我有一个 Wordpress 画廊网站,其中图像在 Bootstrap 5 Modal 中打开,这会打开 Bootstrap Carousel。

图像在带有 php foreach 循环的后循环中加载,每张幻灯片一张。

我正在尝试使用 blowup.js 为每个带有小圆圈的图像添加悬停放大。

我的缩放功能随每张幻灯片正确移动,但只有第一张图像跟踪鼠标所在的位置。随后的放大被卡在左上角。

这是我的功能:

(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);
      

这是该函数在 Carousel 中定位的代码:


<div class="carousel-item">

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

</div>

这是插件中用于跟踪缩放坐标的 JQuery:

  // 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";

    })

【问题讨论】:

    标签: php jquery wordpress bootstrap-5 bootstrap-carousel


    【解决方案1】:

    这是让它工作的最终代码......

         
    /**
     * 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);
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-26
      • 2021-06-12
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多