简体   繁体   English

在鼠标 hover 上放大图像而不使用 Jquery 推送其他图像?

[英]enlarging image on mouse hover without pushing other images using Jquery?

I am trying create the image enlargement effect when you hover your mouse over an image thumbnail like the one that Google Images is using.当您将鼠标悬停在 Google 图片使用的图片缩略图上时,我正在尝试创建图片放大效果。 However, I am encountering a problem where the enlarged image keeps pushing the other image to another location depending on the enlarged image's position.但是,我遇到了一个问题,即根据放大图像的 position,放大图像不断将另一个图像推到另一个位置。

Here's what I have so far:这是我到目前为止所拥有的:

<style>
img{float:left;}
</style>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){
  $("#I1").mouseover(function(){

    $("#I1").animate({height:300,width:300},"fast");
   });
 $("#I1").mouseout(function(){
    $("#I1").animate({height:96,width:128},"fast");
   });
});
</script> 

 <img id="I1" src="http://www.dpstudiolab.com/weblog/wp-content/uploads/2007/11/display-pop-up-2.thumbnail.jpg" >
<img id="I2" src="http://www.dpstudiolab.com/weblog/wp-content/uploads/2007/11/display-pop-up-2.thumbnail.jpg" >

Have you tried giving it a higher z-index than the rest and an absolute position?您是否尝试过给它比 rest 和绝对 position 更高的 z-index? You definitely need to give it an absolute position - this is how you remove it from the DOM stack and make it float there independently without making other elements depend on it.你绝对需要给它一个绝对的 position - 这就是你如何将它从 DOM 堆栈中删除并让它独立浮动在那里而不使其他元素依赖它。

Or, you can play around with clones like I did here:或者,您可以像我在这里做的那样玩弄克隆:

.. removed old url.. .. 删除旧的 url..

Updated with more "images", smoother animation, and removed the bugs from before that used to get an image stuck..更新了更多“图像”,更平滑的 animation,并删除了之前用于卡住图像的错误。

http://jsfiddle.net/Swader/jKTVn/7/ http://jsfiddle.net/Swader/jKTVn/7/

You definitely need to keep it absolutely positioned to remove it from the normal flow.您绝对需要将其保持在绝对位置,以将其从正常流程中移除。 Unfortunately, this also means you need to track the position.不幸的是,这也意味着您需要跟踪 position。 I might recommend duplicating an absolutely positioned div directly on top, give it a higher z-index, then animating the expansion of that div.我可能会建议直接在顶部复制一个绝对定位的 div,给它一个更高的 z-index,然后为该 div 的扩展设置动画。 On mouseout, shink it and then remove the element.在鼠标移出时,将其缩小,然后删除该元素。

<style>
.over {
  /* We use this for all of the generated hover-overs */
  position:absolute;
  z-index:2;
}
img {
  float:left;
  position:relative; /* z-index doesn't work unless position is set */
  z-index:1;
}
</style>

<div id="images">
  <img id="img1" src="foo.jpg">
  <img id="img2" src="bar.jpg">
  <img id="img3" src="baz.jpg">
</div>

<script>
  // First catch the mouse enter, grab position, make new element, append it, then animate
  $("img").mouseenter(function() {
    var top = $(this).position().top;
    var left = $(this).position().left;
    var src = $(this).attr("src");
    $('<div class="over" style="top:'+top+' left:'+left+'" src="'+src+'"></div>')
     .appendTo($("#images"))
     .animate({height:300,width:300},"fast");
  });

  // Note the mouseleave on the hovered element instead of the img
  $(".over").mouseleave(function() {
    var self = this;
    $(this).animate({height:96,width:128},"fast",function() {
      self.remove();
    }
  });
</script>

The effetcs that you get on Google Images is on hover lightbox.您在 Google 图片上获得的效果在 hover 灯箱上。 You can try Suckerfish-Hoverlightbox (see the demo ) for this effect.您可以尝试使用 Suckerfish-Hoverlightbox (请参阅演示)来获得此效果。 Shifting the size of one image will result in change in position of other images.移动一个图像的大小将导致其他图像的 position 发生变化。

See the working demo: http://jsfiddle.net/rathoreahsan/Gsh6B/3/请参阅工作演示: http://jsfiddle.net/rathoreahsan/Gsh6B/3/

You have to take these images in separate DIVs with div { display:inline } style applied.您必须将这些图像放在单独的 DIV 中,并应用div { display:inline }样式。 You have to set z-index property for the both DIVs and the position:relative & position:absolute as I did in my demo.您必须为 DIV 和position:relative & position:absolute设置 z-index 属性,就像我在演示中所做的那样。 Most important is left:130px on the 2nd img div that holds the img at the same position.最重要的是left:130px在第二个 img div 上,它将 img 保存在同一 position 处。

See Another working demo of the solution given by Swader with little modifications:请参阅Swader给出的解决方案的另一个工作演示,稍作修改:

http://jsfiddle.net/rathoreahsan/jKTVn/5/ http://jsfiddle.net/rathoreahsan/jKTVn/5/

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

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