简体   繁体   English

Javascript / jQuery mouseover和mouseout事件监听器

[英]Javascript/jQuery mouseover and mouseout Event Listeners

Not sure how to solve an issue I'm having at the moment. 不确定如何解决我目前遇到的问题。 I'm building a Javascript and PHP powered image gallery. 我正在构建一个Javascript和PHP驱动的图像库。 The basic div structure is that I have a div that contains the gallery as a whole, one for the image being displayed, and a div containing a "previous button", and another containing a "next button." 基本的div结构是,我有一个div,包含整个画廊,一个用于显示图像,另一个div包含“上一个按钮”,另一个包含“下一个按钮”。

The next/previous buttons are positioned on the left/right side at the bottom of the image container div (the gallery container is set to relative position, the button divs to absolute). 下一个/上一个按钮位于图像容器div底部的左侧/右侧(图库容器设置为相对位置,按钮div设置为绝对位置)。 By default they are made non-visible using jQuery, and become visible when you hover over the image container div. 默认情况下,使用jQuery将它们设置为不可见,并在将鼠标悬停在图像容器div上时变为可见。 The issue I'm having is when you hover over the container div, and then over the arrows, the transition effect re-plays and I'm not sure how to fix that using HTML/CSS/JS. 我遇到的问题是,当您将鼠标悬停在容器div上,然后悬停在箭头上时,会重播过渡效果,而且我不确定如何使用HTML / CSS / JS进行修复。 My current structure for the divs is 我目前的div结构是

<div id="galleryContainer">
  <div id="imageContainer">
    <img src="img" />
  </div>
  <div id="leftArrow"></div>
  <div id="rightArrow"></div>
</div>

How do I make it so my fadein/out effect stops acting all bugged when I hover over the next/prev buttons? 当悬停在下一个/上一个按钮上时,如何使淡入/淡出效果停止起作用? I've tried a bunch of combinations of using "onmouseover" listeners when I establish the div, to using jQuery listeners, to trying to change up the hierarchy of the drivs. 建立div时,我尝试了多种组合使用“ onmouseover”侦听器,使用jQuery侦听器,以尝试改变驱动程序的层次结构。 Any help would be appreciated! 任何帮助,将不胜感激!

Edit: Here is my current jQuery code of what I'm trying to do: 编辑:这是我目前正在尝试执行的jQuery代码:

$(document).ready(function(){

$("#imageContainer").mouseover(function()
{
    $("#leftArrow").fadeIn();
    $("#rightArrow").fadeIn();
});

$("#galleryContainer").mouseout(function()
{
    $("#leftArrow").fadeOut();
    $("#rightArrow").fadeOut();
}); 
});

When I put the buttons inside of the imageContainer it still does the fade bug/effect. 当我将按钮放在imageContainer中时,它仍然会执行淡化错误/效果。

You probably want to use $.mouseleave and $mousenter A problem is that the mouseout and mouseover events bubble. 您可能要使用$ .mouseleave$ mousenter问题是mouseout和mouseover事件冒泡。 Then, your handlers are called when those events are fired on #galleryContainer when it happens in any of its descendants 然后,当#galleryContainer中的任何后代发生这些事件时,就会调用您的处理程序

http://jsfiddle.net/z2Y8a/20/ http://jsfiddle.net/z2Y8a/20/

$("#imageContainer").mouseenter(function() {
    $("#leftArrow").fadeIn();
    $("#rightArrow").fadeIn();
});

$("#galleryContainer").mouseleave(function() {
    $("#leftArrow").fadeOut();
    $("#rightArrow").fadeOut();
}); 

This is how I interpreted your question without seeing any code.. Let me know. 这就是我如何解释您的问题而没有看到任何代码的方式。让我知道。
I added colors to the divs so you can see which is located where. 我为div添加了颜色,因此您可以看到哪个位于哪里。

jsFiddle: http://jsfiddle.net/z2Y8a/19/ jsFiddle: http : //jsfiddle.net/z2Y8a/19/

<div id="galleryContainer" style="width:200px;height:200px;background:green;">
    <div id="imageContainer" style="width:50px;height:100px;background:blue;">
        <div style="height:75px;">
            <img src="img" />
        </div>
        <div id="leftArrow" style="width:25px;height:25px;background:red;float:left;display:none;">L</div>
        <div id="rightArrow" style="width:25px;height:25px;background:yellow;float:left;display:none;">R</div>
    </div>
</div>​

<script>
    $("#imageContainer").hover( function()
    {
        $("#leftArrow").toggle();
        $("#rightArrow").toggle(); 
    });​
</script>

-- EDIT -- -编辑-

$("#galleryContainer").hover(function()
{
    $("#leftArrow").toggle();
    $("#rightArrow").toggle();
});

It would not flicker if your arrows are inside of your image container. 如果箭头位于图像容器内部,则不会闪烁。 Or maybe put the show arrow effect in the main gallery container. 或者,可以将显示箭头效果放入主图库容器中。 Basically when your mouse laves the image container, it will trigger the mouseout. 基本上,当鼠标从属图像容器时,它将触发鼠标移出。 When I say leave I mean in code sense, visually it does not leave because you have it positioned differently but in code sense, your mouse left the image container and entered the arrow containers 当我说“离开”时,我的意思是在代码意义上,从视觉上讲它不会离开,因为您将其放置在不同的位置,但是在代码意义上,鼠标离开了图像容器并进入了箭头容器

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

相关问题 高亮显示功能(鼠标悬停/鼠标悬停事件监听器)不起作用 - Highlight function (mouseover/mouseout event listeners) not working 事件监听器-MouseOver,Mouseout,HTML行的OnClick - Event Listeners - MouseOver, Mouseout, OnClick for HTML Rows 循环Jquery + mouseover / mouseout事件 - circulate Jquery + mouseover/mouseout event 鼠标悬停/鼠标悬停时未定义jQuery事件 - jQuery event not defined on mouseover/mouseout 使用mouseover和mouseout事件监听器突出显示页面元素的问题 - Issue with highlighting page elements using mouseover and mouseout event listeners jQuery闭包和事件函数(鼠标悬停,鼠标移出,…) - jQuery closures and event function (mouseover, mouseout, …) 鼠标移出后触发jQuery Ajax mouseover事件 - jQuery Ajax mouseover event firing after mouseout JavaScript:DRY mouseover / mouseout事件处理程序 - JavaScript: DRY mouseover/mouseout event handlers Javascript:“鼠标悬停”和“鼠标悬停”事件处理程序有效,但当“鼠标悬停”被“单击”替换时“鼠标悬停”无法正常工作 - Javascript: "Mouseover" and "mouseout" event handlers work, but "mouseout" doesn't work properly when "mouseover" is replaced by "click" jQuery mouseover和mouseout问题 - Jquery mouseover and mouseout issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM