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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)