简体   繁体   English

用PHP悬停显示图像

[英]Show image on hover with PHP

I have a small problem with my PHP code and It would be very nice if someone could help me. 我的PHP代码有一个小问题,如果有人可以帮助我,那将是非常好的。 I want to display an image when hovering over a link. 我想在鼠标悬停在链接上时显示图像。 This is the link with the PHP code that I have now: 这是我现在拥有的PHP代码的链接:

<a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) {the_post_thumbnail();} else if ( has_post_video() ) {the_post_video_image();}?></a>

This code shows a image, but I want to execute this code when hovering over the link with the image: 此代码显示了一个图像,但我想在将鼠标悬停在带有图像的链接上时执行此代码:

<?php echo print_image_function(); ?>

The code also shows a image that belongs to a category. 该代码还显示属于某个类别的图像。 I don't want the initial image to disappear I simply want to show the second image on top off the first image when hovering over the first image. 我不希望初始图像消失我只是想在第一张图像上方悬停在第一张图像上时显示第二张图像。

I don't know if it is helpful but I use Wordpress and I am not a PHP expert. 我不知道它是否有用,但我使用的是Wordpress,而且我不是PHP专家。 I even don't know if this is going to work. 我甚至不知道这是否会起作用。 Thats why I am asking if somebody can help me with this. 这就是为什么我问是否有人可以帮我这个。

Thanks in advance 提前致谢

THANKS EVERYONE 感谢大家

I want to thank everybody that took the time to read my post and helped me by giving their solution. 我要感谢所有花时间阅读我的帖子并通过提供解决方案帮助我的人。 I didnt exspect so many answers in such a fast time. 在如此快的时间里,我没有想到这么多答案。 After spending a few hours trying it to get it to work with PHP, CSS and Javacript, I stumbled upon the following question on this website: Solution 花了几个小时试图让它与PHP,CSS和Javacript一起工作后,我在这个网站上偶然发现了以下问题: 解决方案

It was exactly where I was looking for and with a few modifications to fit my needs, I got it to work. 这正是我寻找的地方,经过一些修改以满足我的需求,我开始工作了。 Sometimes things can be so easy while you are looking for the hard way. 有时候,在寻找艰难的方式时,事情会变得如此简单。 So for everyone that has the same problem: You can use one of the solutions that where given by the awesome people here or take a look at the link above. 因此,对于遇到相同问题的每个人:您可以使用其中一个解决方案,这些解决方案由优秀人员在此处提供,或者查看上面的链接。

Thanks again! 再次感谢! :) :)

You can do this with CSS (if you so please and this fits with your overall architecture) - here is an example using the :hover condition and :after pseudo element. 您可以使用CSS执行此操作(如果您愿意,这符合您的整体架构) - 这是一个使用:hover条件和:after伪元素的示例。
html HTML

<a href="#" class="foo"><img src="http://www.gravatar.com/avatar/e5b801f3e9b405c4feb5a4461aff73c2?s=32&d=identicon&r=PG" /></a>​

css CSS

.foo {
    position: relative;
}
.foo:hover:after {
    content: ' ';
    background-image: url(http://www.gravatar.com/avatar/ca536e1d909e8d58cba0fdb55be0c6c5?s=32&d=identicon&r=PG);
    position: absolute;
    top: 10px;
    left: 10px;
    height: 32px;
    width: 32px;   
}​

http://jsfiddle.net/rlemon/3kWhf/ demo here http://jsfiddle.net/rlemon/3kWhf/ demo这里

Edit: 编辑:

Always when using new or experimental CSS features reference a compatibility chart http://caniuse.com/ to ensure you are still in your supported browsers. 总是在使用新的或实验性的CSS功能时,请参考兼容性图表http://caniuse.com/,以确保您仍在支持的浏览器中。 For example, :after is only supported starting IE8. 例如, :after仅支持启动IE8。

You cannot randomly execute server side code on the client side. 不能在客户端随机执行服务器端代码。

Try using javascript AJAX requests instead. 尝试使用javascript AJAX请求。

PHP is a server-side language; PHP是一种服务器端语言; you can't get PHP to execute after the page has loaded (because PHP completely finishes parsing before the page loads). 在页面加载后你无法让PHP执行(因为PHP在页面加载之前完全解析)。 If you want hover events, you need JS. 如果你想要悬停事件,你需要JS。

Firstly you don't need the elseif statement. 首先,您不需要elseif语句。 An else will serve the same purpose, unless you intend to have blank tags where neither a thumbnail or a video image are present. 除非您打算在没有缩略图或视频图像的情况下使用空白标签,否则else将用于相同的目的。

<a href="<?php the_permalink(); ?>">
    <?php 
        if ( has_post_thumbnail() ) 
        {
            the_post_thumbnail();
        } 
        else
        {
            the_post_video_image();
        }
    ?>
</a>

You can't explicitly use PHP for client side functionality. 您无法明确使用PHP作为客户端功能。 You will need to use javascript or jquery to supply the on hover capability. 您需要使用javascript或jquery来提供on hover功能。

Jquery example: Jquery示例:

$(function() {

    $('a').hover(function() {

        // Code to execute whenever the <a> is hovered over

        // Example: Whenever THIS <a> tag is hovered over, display
        //          the hidden image that has a class of .rollover

        $(this + ' .rollover').fadeIn(300);

    }, function() {

        // Execute this code when the mouse exits the hover area
        // Example (inline with above example)

        $(this + ' .rollover').fadeOut(300);

    });

});

To have an image placed on top of another image you would need to make sure your CSS uses absolute positioning for the images with the image that is to overlay the other on hover is given a z-index value higher than the image to sit underneath it. 要将图像放置在另一个图像的顶部,您需要确保您的CSS使用图像的absolute定位,并且在悬停时要叠加另一个图像的图像的z-index值高于位于其下方的图像。

Hope this helps. 希望这可以帮助。

您需要一些JavaScript和/或CSS来实现这一点,因为PHP位于服务器端,而不是客户端浏览器。

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

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