简体   繁体   English

将鼠标悬停在div上时弹出图像(Jquery)

[英]Popup Image when hovering over a div (Jquery)

I need some help. 我需要协助。 I want to make an image appear where the mouse is located over a div. 我想让图像出现在鼠标位于div上方的位置。 There is no code that I can show as I've failed many times at achieving this. 我没有代码可以展示,因为我多次失败都没能实现这一目标。

Thanks. 谢谢。

From you original question and this comment ("Hello, thanks for the reply. The image needs to pop up over the div about the mouse point. Moving the mouse changes the image location to where the mouse is. Thanks though!") 从你原来的问题和这个评论(“你好,感谢回复。图像需要弹出关于鼠标点的div。移动鼠标将图像位置更改为鼠标所在的位置。非常感谢!”)

I created this solution: 我创建了这个解决方案

http://jsfiddle.net/YPu96/1/ http://jsfiddle.net/YPu96/1/

Hover over div: The image becomes visible and follows the mouse-pointer. 将鼠标悬停在div上:图像变为可见并跟随鼠标指针。

On click: The image becomes invisible and stops following. 单击:图像变为不可见并停止跟随。

HTML: HTML:

<div class="someDiv">
  <p>Foobar</p>
</div>
<img style="display:none" class="image" src="http://www.budick.eu/images/logo-BudickEu.jpg"/>

JavaScript: JavaScript的:

$(".someDiv").hover(function() {
  $(document).mousemove(function(event) {
    $(".image").css({"position":"absolute","left":event.clientX ,"top":event.clientY     }).show();    
  });    
});

//end movement with click
$(document).bind("click",function(){
  $(document).unbind("mousemove");
  $(".image").hide();
});

You definitely don't need to use javascript. 你绝对不需要使用javascript。 This works on the latest version of Chrome, use vendor prefixing for transitions. 这适用于最新版本的Chrome,使用供应商前缀进行转换。

http://jsfiddle.net/pKYu2/16/ http://jsfiddle.net/pKYu2/16/

HTML HTML

<p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    <span id="mouseOver"><img src="http://placekitten.com/120/120">Mouse Over This</span>     
</p>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

CSS CSS

#mouseOver {
    display: inline;
    position: relative;
}

#mouseOver img {
    position: absolute;
    left: 50%;
    transform: translate(-50%);
    bottom: 1em;
    opacity: 0;
    pointer-events: none;
    transition-duration: 800ms;    
}

#mouseOver:hover img {
    opacity: 1;
    transition-duration: 400ms;
}

You can use the hover event: 您可以使用hover事件:

$("div").hover(function() {
    $("img").show();
}, function() {
    $("img").hide();
});

http://jsfiddle.net/pKYu2/2/ http://jsfiddle.net/pKYu2/2/

This will replace text 这将取代文字

$(document).ready(function(){
   $('#mouseOver').mouseover(function(e){
        $('#imageReplace').empty()
        $('#imageReplace').append("<img src=\"http://placekitten.com/120/120\" />"); 
    })
})

The empty function isn't really necessary, but I find to be good practice, as it's very easy to forget to unbind events from the DOM if you don't make liberal use of .empty and .remove empty函数不是必需的,但我发现这是一个很好的练习,因为如果不自由使用.empty.remove 容易忘记从DOM解除绑定事件

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

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