简体   繁体   English

Woocommerce缩略图交换

[英]Woocommerce Thumbnail Image Swap

I am working on a custom WordPress theme and I have added the function of swapping the main image with the thumbnail image on hover see here using the following JS: 我正在研究自定义WordPress主题,并且添加了以下功能:将主图像与悬停时的缩略图交换, 请参见此处,使用以下JS:

jQuery(document).ready(function($) {
 // Get the main WC image as a variable
 var wcih_main_imgage = $( 'a.woocommerce-main-image' ).attr( 'href' );
 // This is what will happen when you hover a product thumb
 $(".thumbnails a").hover(
 // Swap out the main image with the thumb
   function(){
     var photo_fullsize = $(this).attr('href');
     $('.woocommerce-main-image img').attr('src', photo_fullsize);
   },
   // Return the main image to the original
   function(){
     $('.woocommerce-main-image img').attr('src', wcih_main_imgage);
   }
 );
} );

This works but returns images that are bigger than the main image. 此方法有效,但返回的图像大于主图像。 Now I can manage this problem in other areas of the site by using this code: 现在,我可以使用以下代码在网站的其他区域中解决此问题:

<script>
                jQuery(document).ready(function() { jQuery('.overlayimage').css('height','288px'); });

                    jQuery('.overlayimage').css('width','288px')
                    jQuery('.overlayimage').css({'overflow':'hidden', 'left':'0', 'top':'0', 'position':'absolute'});

                function resizeoverlay() {
                    var pr = jQuery('.overlayimage').eq(0).prev();
                    jQuery('.overlayimage').css('height',pr.height());
                    jQuery('.overlayimage').css('width',pr.width());

                    }
                jQuery(window).load(resizeoverlay);
                jQuery(window).resize(resizeoverlay);
            </script>

... but I am struggling to get the two functions working correctly. ...但是我正在努力使这两个功能正常工作。 Can anyone point me in the right direction? 谁能指出我正确的方向?

you should do all your operations in jquery dom ready function like that 你应该在jQuery dom ready函数中进行所有操作

       <script>
                jQuery(document).ready(function() { 
                   jQuery('.overlayimage').css('height','288px'); 
                   jQuery('.overlayimage').css('width','288px');
                   jQuery('.overlayimage').css({'overflow':'hidden', 'left':'0', 'top':'0', 'position':'absolute'});

                   function resizeoverlay() {
                       var pr = jQuery('.overlayimage').eq(0).prev();
                       jQuery('.overlayimage').css('height',pr.height());
                       jQuery('.overlayimage').css('width',pr.width());
                   }
                   jQuery(window).load(resizeoverlay());
                   jQuery(window).resize(resizeoverlay());
                });
       </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM