简体   繁体   English

悬停时替换图像并单击链接

[英]Replace Image on Hover & Click of a Link

I'm trying to set up a page that looks like the image below. 我正在尝试建立一个如下图所示的页面。

Here's the part that;s hanging me up... 这是使我挂断的部分...

I would like to have the main image replaced when I "hover" or "click" on one of the links below it. 当我在其下面的链接之一上“悬停”或“单击”时,我想替换主图像。 Each link below the main image would have it's own specific image. 主图像下方的每个链接都有其自己的特定图像。 Then I would like to make the link images "toggle" on/off. 然后,我想使链接图像“切换”开/关。

I have tried to do this with pure css and and having a really hard time. 我试图用纯CSS来做到这一点,并且遇到了很多困难。

在此处输入图片说明

Thanks for your help. 谢谢你的帮助。

JavaScript solution JavaScript解决方案

HTML: HTML:

<div id="output"></div>
<ul id="nav">
    <li data-href="http://placekitten.com/120/100">Cat 1</li>
    <li data-href="http://placekitten.com/110/100">Cat 2</li>
    <li data-href="http://placekitten.com/130/100">Cat 3</li>
    <li data-href="http://placekitten.com/150/100">Cat 4</li>
</ul>

JavaScript: JavaScript:

$( nav ).on( 'click', 'li', function () {
    $( this ).addClass( 'selected' ).siblings().removeClass( 'selected' );
    var url = $( this ).data( 'href' );
    $( output ).html( '<img src="' + url + '">' );
});

Live demo: http://jsfiddle.net/Kne3d/ 现场演示: http //jsfiddle.net/Kne3d/


Alternative solution (pure CSS) 替代解决方案(纯CSS)

HTML: HTML:

<div id="output">
    <img id="image1" src="http://placekitten.com/120/100">
    <img id="image2" src="http://placekitten.com/130/100">
    <img id="image3" src="http://placekitten.com/110/100">
    <img id="image4" src="http://placekitten.com/140/100">
</div>

<ul id="nav">
    <li><a href="#image1">Cat 1</a></li>
    <li><a href="#image2">Cat 2</a></li>
    <li><a href="#image3">Cat 3</a></li>
    <li><a href="#image4">Cat 4</a></li>
</ul>

CSS: CSS:

#output img {
    display: none;
}

#output img:target {
    display: inline;
}

Live demo: http://jsfiddle.net/Kne3d/3/ 现场演示: http //jsfiddle.net/Kne3d/3/


Third solution (pure CSS, on hover) 第三种解决方案(纯CSS,悬停)

HTML: HTML:

<ul id="gallery">
    <li>
        <span>Cat 1</span>
        <img src="http://placekitten.com/120/100">
    </li>
    <li>
        <span>Cat 2</span>
        <img src="http://placekitten.com/150/100">
    </li>
    <li>
        <span>Cat 3</span>
        <img src="http://placekitten.com/110/100">
    </li>
    <li>
        <span>Cat 4</span>
        <img src="http://placekitten.com/140/100">
    </li>
</ul>

CSS: CSS:

#gallery img {
    display: none;
}

#gallery span:hover ~ img {
    display: block;
}

Live demo: http://jsfiddle.net/Kne3d/5/ 现场演示: http //jsfiddle.net/Kne3d/5/

Your links below the main image should be contained in some identifiable container we can reference, like so: 您在主图片下方的链接应包含在我们可以引用的可识别容器中,如下所示:

<img id="mainImage"></img>

<ul id="mainImageLinks">
   <li><a href="image1.jpg" />Link 1</a></li>
   <li><a href="image2.jpg" />Link 1</a></li>
   <li><a href="image3.jpg" />Link 1</a></li>
   <li><a href="image4.jpg" />Link 1</a></li>
   <li><a href="image5.jpg" />Link 1</a></li>
</ul>

Then you would use CSS to style the elements, obviously, and which is beyond the scope of your question, but would be something like this: 然后,很明显,您将使用CSS来设置元素的样式,这超出了您的问题范围,但将是这样的:

#mainImage {
   width: 200px;
   height: 200px;
   border: 1px solid black;
   display: block;
   margin: 10px; 
}

#mainImageLinks a {
   display: block;
   float: left;
   padding: 3px; 
}

Then jQuery takes care of the rest: 然后jQuery负责其余的工作:

$('#mainImageLinks li a').bind('mouseenter', function() {
   var url = $(this).attr('href');
   $('#mainImage').attr('src', url);
   return false; // prevent the default action on click
});

And here is a working example . 这是一个可行的例子

You can do the hover change in CSS with a sprite, :hover , and the background-position property but I don't think that would be ideal for this type of thing. 您可以使用sprite :hoverbackground-position属性在CSS中进行悬停更改,但我认为这对此类事情并不理想。 A jQuery solution would be better. jQuery解决方案会更好。

I'm not sure how to do this with just css, but here is a quick little jquery that should get you close. 我不确定如何仅使用CSS来执行此操作,但是这里有一个快速的小jQuery,可以使您接近。

$("a").each(function(index) {
    $(this).click(function() {
        $("#mainImg").find("img").attr('src', $(this).find("img").attr('src'));
    });
});

It would help if you posted some actual code, that way we could reference it when giving you solutions. 如果您发布了一些实际的代码,这将有所帮助,这样我们可以在为您提供解决方案时引用它。 My code assumes that you have an anchor that holds each image. 我的代码假定您有一个保存每个图像的锚点。

Try using this guide. 尝试使用本指南。 Image Change on Hover . 悬停时的图像更改 It has implementation of mousehover. 它具有鼠标悬停的实现。 You will only need to change the background: url instead of background-position being done. 您只需要更改背景:url即可完成背景位置。

And this link is for your click sort of like a full implementation On MouseClick . 而且此链接供您单击,就像在MouseClick上的完整实现一样。 You actually only need to register a click event you want to go through the css route. 实际上,您只需要注册要通过CSS路径进行的click事件。

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

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