dongshi4773 2014-12-13 18:24
浏览 80
已采纳

Wordpress多个特色图像悬停帮助需要(php / jquery) - 认为我80%那里

I wanted to have the featured header images in my blog posts change to alternative images on hover - all of my header images are black and white so the idea is to have them change to high contrast monotone versions when hovering...

I followed the guidance of this tutorial, and after a few bumps in the road I'm now at the final hurdle where I add some javascript within the head tag of a primary php file in order to generate the hovering image transition.

Unfortunately it does absolutely nothing - I've tried placing the code in various other places (including other primary php files), with no success. Since my coding knowledge is quite basic and only within the realms of html/css, I'm really not sure what else I can try to get this working...

Unfortunately the demo link within the original tutorial I followed is dead so I can't pull apart any working code either!

this is my blog - I've temporarily removed display:none on the alternative image, just to give you a better idea of what I'm trying to do. The following is the javascript code in question:

<script type="text/javascript">
jQuery(document).ready(function() {

//Get the total number of elements having the class "galery"
var getTotalElements = jQuery(".gallery").length;

//Running Loop
for(var i = 1; i <= getTotalElements; i++) {
jQuery('.post'+i).on('hover', createCallback(i) );
jQuery('.post'+i).on('mouseleave', createCallback1(i) );
}

//This method is for hover function
function createCallback( i ){
return function(){
jQuery('.post'+i+' .entry-header .FirstImage'+i).hide();
jQuery('.post'+i+' .entry-header .SecondImage'+i).show();
}
}

//This method is for mouseleave function.
function createCallback1( i ){
return function(){
jQuery('.post'+i+' .entry-header .FirstImage'+i).show();
jQuery('.post'+i+' .entry-header .SecondImage'+i).hide();
}
}

});
</script>

The following is the code I've included in my content php file, which calls on both featured images:

<a href="<?php the_permalink(); ?>">
<div  class="first_image FirstImage"><?php the_post_thumbnail(); ?></div>
<div class="second_image SecondImage"><?php echo MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'colored-image', get_the_ID()); ?></div>
</a>
  • 写回答

1条回答 默认 最新

  • doucan1979 2014-12-13 18:38
    关注

    First modify the script to something like this instead.

    jQuery(function($) {
    
        $('[class^="post"]').on('mouseenter mouseleave', function(e) {
            var flag = e.type === 'mouseenter';
            $('.entry-header .FirstImage', this).toggle(!flag);
            $('.entry-header .SecondImage', this).toggle(flag);
        });
    
    });
    

    Then you have to actually include the script after jQuery.
    Right now you have the script in the header, and jQuery is loaded in the footer, so jQuery isn't available when the script is loaded.

    In Wordpress, you should generally put the script in a seperate file, and enqueue it with wp_enqueue_script

    It should be noted that just a straight swap like this could probably be done with just CSS as well

    .post:hover .entry-header .FirstImage {
        display: none;
    }
    
    .post:hover .entry-header .SecondImage {
        display: inline;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?