【问题标题】:Wordpress multiple featured image hover help needed (php/jquery) - think I'm 80% there需要 Wordpress 多个特色图像悬停帮助(php/jquery) - 认为我在那里 80%
【发布时间】:2014-12-13 18:24:30
【问题描述】:

我想让我的博客文章中的特色标题图像在悬停时更改为替代图像 - 我所有的标题图像都是黑白的,所以我的想法是让它们在悬停时更改为高对比度单调版本...

我遵循this tutorial 的指导,经过几次颠簸之后,我现在处于最后一个障碍,我在主 php 文件的 head 标记中添加一些 javascript 以生成悬停图像过渡.

不幸的是,它什么也没做——我尝试将代码放在其他不同的地方(包括其他主要的 php 文件),但没有成功。由于我的编码知识非常基础,并且只在 html/css 领域内,我真的不确定我还能尝试什么来让这个工作......

不幸的是,我遵循的原始教程中的演示链接已失效,因此我也无法分解任何工作代码!

this is my blog - 我暂时删除了替代图像上的 display:none,只是为了让您更好地了解我想要做什么。以下是有问题的javascript代码:

<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>

以下是我在内容 php 文件中包含的代码,它调用了两个特色图像:

<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>

【问题讨论】:

    标签: javascript php jquery wordpress hover


    【解决方案1】:

    首先将脚本修改为类似的内容。

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

    然后你必须在 jQuery 之后实际包含脚本。
    现在你在页眉中有脚本,并且在页脚中加载了 jQuery,因此加载脚本时 jQuery 不可用。

    在 Wordpress 中,您通常应该将脚本放在一个单独的文件中,并使用 wp_enqueue_script 将其排入队列

    应该注意,像这样的直接交换也可能只使用 CSS 来完成

    .post:hover .entry-header .FirstImage {
        display: none;
    }
    
    .post:hover .entry-header .SecondImage {
        display: inline;
    }
    

    【讨论】:

    • 天哪!我整天都在尝试破解 jQuery 的东西——只要输入你的 css 代码,它就可以工作了!非常感谢。当我悬停时,其他帖子标题现在正在起作用,但我认为这只是因为我还没有真正将替代版本上传给他们......
    • 我看着他们,第二张图片不见了,所以他们就消失了。我并没有真正测试 CSS,但如果您需要更具体,您应该能够调整选择器、添加类等。
    • 是的,对我来说,悬停时所有其他标题图像也会消失。我想这可能是因为我实际上并没有上传任何替代品,但我刚刚上传了第二篇文章的替代品(吟游诗人的图片),它仍然消失了……啊,伙计!好吧,至少我现在知道 CSS 是可能的......
    • 好的,已修复!这是我的错误 - 我使用今天早些时候尝试的旧插件选择了第二个替代图像!使用正确的选择它并且它可以工作。非常感谢您的帮助,这让浪费的一天变得更好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多