简体   繁体   English

当我将鼠标悬停在另一个图像上时,使图像可见

[英]Make an image visible when I hover over another

Essentially I have an interactive map which contains 4 div statements each of which contains an image of an island. 本质上,我有一个交互式地图,其中包含4个div语句,每个语句包含一个岛的图像。 I would like to create an on hover event which will display a corresponding sailing timetable depending on which image the user hovers. 我想创建一个悬停事件,该事件将根据用户悬停的图像显示相应的航行时间表。 eg island 1 should display timetable 1. 例如,岛屿1应该显示时间表1。

I have the following code so far and ideally I am looking for a javascript or css solution: 到目前为止,我有以下代码,理想情况下,我正在寻找JavaScript或CSS解决方案:

            <div class="Map">
                <div id="Island_Morar">
                    <img src="images/IsleOfMorar.jpg"/>
                </div>

                <div id="Island_Rum">
                    <img src="images/IsleOfRum.jpg"/>
                </div>                      

                <div id="Island_Eigg">
                    <img src="images/IsleOfEigg.jpg"/>
                </div>

                <div id="Island_Muck">
                    <img src="images/IsleOfMuck.jpg"/>
                </div>


            </div>  

            <img id="TimetableEigg" src="images/TimetableEigg.jpg">

any help is appreciated. 任何帮助表示赞赏。

You need some different markup if you want a plain css solution. 如果需要纯CSS解决方案,则需要一些不同的标记。 If you want to have different timetables for each hover you should go with something like this: 如果您希望每个悬停都有不同的时间表,则应使用以下内容:

markup 标记

    <div class="tt-container" id="Island_Rum">
        <img src="images/IsleOfRum.jpg"/>
        <img class="timetable" src="images/TimetableRum.jpg">
    </div>                      

    <div class="tt-container" id="Island_Eigg">
        <img src="images/IsleOfEigg.jpg"/>
        <img class="timetable" src="images/TimetableEigg.jpg">
    </div>

    <div class="tt-container" id="Island_Muck">
        <img src="images/IsleOfMuck.jpg"/>
        <img class="timetable" src="images/TimetableMuck.jpg">
    </div>
</div>  

css 的CSS

.timetable { 
    display : none; 
}

.tt-container:hover .timetable { 
    display : block; 
}

That should do the trick 这应该够了吧

If you want to keep your current HTML code, I'd make three image blocks for timetables, and initially set them all to display: none; 如果要保留当前的HTML代码,我将为时间表创建三个图像块,并首先将它们全部设置为显示: and add onmouseover event handlers to island elements which would contain Javascript statement which will set disply: block; 并将onmouseover事件处理程序添加到包含将设置disply:block;的Javascript语句的岛元素。 on appropriate timetable. 在适当的时间表上。

Something like this: 像这样:

<div class="Map">
    <div id="Island_Morar" onmouseover="document.getElementById('TimetableEigg1').style.display = 'block';">
        <img src="images/IsleOfMorar.jpg"/>
    </div>

    <div id="Island_Rum" onmouseover="document.getElementById('TimetableEigg2').style.display = 'block';" >
        <img src="images/IsleOfRum.jpg"/>
    </div>                      

    <div id="Island_Eigg" onmouseover="document.getElementById('TimetableEigg3').style.display = 'block';" >
        <img src="images/IsleOfEigg.jpg"/>
    </div>

    <div id="Island_Muck" onmouseover="document.getElementById('TimetableEigg4').style.display = 'block';" >
        <img src="images/IsleOfMuck.jpg"/>
    </div>


</div>  

<img id="TimetableEigg1" src="images/TimetableEigg1.jpg">
<img id="TimetableEigg2" src="images/TimetableEigg2.jpg">
<img id="TimetableEigg3" src="images/TimetableEigg3.jpg">
<img id="TimetableEigg4" src="images/TimetableEigg4.jpg">

Seems you barely know the basics of HTML and already trying to jump too deep. 似乎您几乎不了解HTML的基础知识,并且已经尝试过深。 External libraries will help you and speed up your progress. 外部库将帮助您并加快进度。 I see people gave you CSS solutions so here is a JS solution. 我看到有人给您CSS解决方案,所以这里是一个JS解决方案。 First thing is download the well known JS library called jQuery . 首先是下载众所周知的JS库jQuery then load this file to your page and add a script at the bottom of your body tag: 然后将此文件加载到您的页面,并在body标签底部添加一个脚本:

$("div.map").on("mouseover", "#Island_Morar", function(e) {
    $(this).show(); // option one
    //$(this).addClass("class-name"); // option two
}).on("mouseout", "#Island_Morar", function(e) {
    $(this).hide(); // option one
    //$(this).removeClass("class-name"); // option two
});

With this script you can do whatever you want, for example - use the second option of adding and removing classes in order to animate your Timetables (see Example ). 使用此脚本,您可以执行任何所需的操作,例如-使用添加和删除类的第二个选项来设置时间表的动画(请参见示例 )。

Pure CSS solution but you need to place the large image in .main div 纯CSS解决方案,但您需要将大图像放在.main div中

the first image will be displayed first and will change on hover on other images and when you leave move out of the main div it will show the first image 第一个图像将首先显示,并且将在其他图像上悬停时发生变化,当您离开主div时,它将显示第一个图像

Note: used random images 注意:使用的随机图片

 .Map > div { display: inline-block; } img.two, img.three, img.four, #Island_Rum:hover ~ img.one, #Island_Muck:hover ~ img.one, #Island_Eigg:hover ~ img.one { display: none; } img.one { display: block; } #Island_Morar:hover ~ img.one { display: block; } #Island_Rum:hover ~ img.two { display: block; } #Island_Eigg:hover ~ img.three { display: block; } #Island_Muck:hover ~ img.four { display: block; } 
 <div class="Map"> <div id="Island_Morar"> <img src="http://placeimg.com/100/100/any/animals" /> </div> <div id="Island_Rum"> <img src="http://placeimg.com/100/100/any/arch" /> </div> <div id="Island_Eigg"> <img src="http://placeimg.com/100/100/any/nature" /> </div> <div id="Island_Muck"> <img src="http://placeimg.com/100/100/any/tech" /> </div> <img class="one" src="http://placeimg.com/400/400/any/animals" /> <img class="two" src="http://placeimg.com/400/400/any/arch" /> <img class="three" src="http://placeimg.com/400/400/any/nature" /> <img class="four" src="http://placeimg.com/400/400/any/tech" /> </div> 

Possible CSS / JQuery solution: 可能的CSS / JQuery解决方案:

 $(".Map a").hover( function() { $('#' + $(this).attr('class')).show(); }, function() { $('#' + $(this).attr('class')).hide(); } ); 
 .timetables img { display:none; } 
 <div class="Map"> <a href="#" class="islandmorar"> <img src="images/IsleOfMorar.jpg"/> </a> <a class="islandrum"> <img src="images/IsleOfRum.jpg"/> </a> <a class="islandeigg"> <img src="images/IsleOfEigg.jpg"/> </a> <a class="islandmuck"> <img src="images/IsleOfMuck.jpg"/> </a> </div> <div class="timetables"> <img id="islandmorar" src="images/TimetableEigg.jpg"/> <img id="islandrum" src="images/TimetableEigg.jpg"/> <img id="islandeigg" src="images/TimetableEigg.jpg"/> <img id="islandmuck" src="images/TimetableEigg.jpg"/> </div> 

Don't put class="map" to the wrapper div, give it to every div with id beginning with "Island_...". 不要将class =“ map”放到包装div上,而是将其分配给ID以“ Island _...”开头的每个div。 Do the same with your timeTable images, give them a class "timeTable". 对您的时间表图像进行相同的操作,为它们提供“时间表”类。

Put this before your "head" end tag : 将其放在您的“ head”结束标记之前:

<script>
"use strict";

//wait for every element to be loaded
window.onload = function(){initialization();}
</script>

Then, put this before your "body" end tag : 然后,将其放在您的“ body”结束标记之前:

<script>
"use strict";

//first create a function that hides elements with class 'timeTable'
function hide(elements){
    var htmlClass = document.getElementsByClassName(elements);
    //hide every element with class
    for (var i = 0 ; i < htmlClass.length ; i++){
        htmlClass[i].style.display = "none";
        htmlClass[i].style.visibility = "hidden";
    }   
}

//create a function that show only the timeTable you want
function show(element){
    document.getElementById(element).style.display = "block";
    document.getElementById(element).style.visibility = "visible";
}

function initialization(){
    //replace 'someMapId' with the id of the image you are hovering
    //replace 'someTimeTableId' with the id of the image you want to show
    //replace 'timeTable' with the name of a class you want to hide
    document.getElementById("someMapId").onmouseover = function(){
        hide("timeTable");
        show("someTimeTableId");
    }
    //repeat these 3 lines for every image the user will hover
}
</script>

Don't forget the quotes when using the functions. 使用函数时不要忘记引号。 You should use css for styling and javascript for interactions. 您应该使用css进行样式设置,并使用javascript进行交互。 You don't need jQuery for basic scripts like that, it only slows page loading and keeps you away from learning basic javascript. 您不需要jQuery这样的基本脚本,它只会减慢页面加载速度,并使您无需学习基本的javascript。

(Ok, I edited mistakes, now it works ;) (好的,我编辑了错误,现在可以了;)

jsFiddle jsFiddle

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

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