简体   繁体   English

使用javascript在鼠标悬停时查看内容顶部的放大图像

[英]view enlarged image ontop of content on mouseover with javascript

I am trying to use javascript to show a larger version of my image on mouseover. 我试图使用javascript在鼠标悬停时显示较大的图像版本。 I have the functionality working however the display property is not right and I need the image to be shown ontop of the page content ie like a popout. 我具有正常工作的功能,但是display属性不正确,我需要将图像显示在页面内容的顶部,例如弹出窗口。

How an I add this to my code. 我如何将其添加到我的代码中。 The problem I have is that my page is divided into columns so the image is restricted by the width of the column so I need a way to override that ie use a popout. 我的问题是我的页面分为几列,因此图像受到列宽的限制,因此我需要一种方法来覆盖它,即使用弹出窗口。

DEMO - js fiddle 演示-js小提琴

html: 的HTML:

<div class="column1">
<div class="enlarge">
<img id="test" src="example.jpg" onmouseover="bigImg(this)" onmouseout="normalImg(this)" width="400" height="300" class="img-responsive" alt="image">
<div id="test1" class="test1" style="display:none;">
<img width="800" src="example.jpg" alt="" />
</div>
</div>
</div>
<div class="column2">
A LOT OF TEXT IN HERE
</div>

javascript: javascript:

function bigImg() {
            $("#test1").show();
        }

        function normalImg() {
            $("#test1").hide();
        }

It would be restrictive to code your script using specific elements (IDs). 使用特定元素(ID)编写脚本将受到限制。 Would advice you to use element classes instead. 建议您改用元素类。

You can achieve what you want several ways, here is one using absolute positioning for the big image: 您可以通过几种方法实现所需的目标,这是对大图像使用绝对定位的一种方法:

 $('.small-image').on('mouseenter', function(e) { $(this).siblings('.big-image').show(); }).on('mouseleave', function(e) { $(this).siblings('.big-image').hide(); }) 
 body { overflow-x: hidden; } .row::after { content: ' '; display: block; clear: both; } .column { float: left; } .column1 { width: 20%; } .column2 { width: 80%; } img { max-width: 100%; height: auto; display: block; } .enlarge { position: relative; } .big-image { display: none; position: absolute; left: 100%; top: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row"> <div class="column column1"> <div class="enlarge"> <img src="http://lorempixel.com/400/300/" class="small-image" alt="image"> <div class="big-image"> <img src="http://lorempixel.com/400/300/" alt="" /> </div> </div> </div> <div class="column column2"> A LOT OF TEXT IN HERE </div> </div> 

Also on Fiddle . 同样在小提琴上

Try putting the img tag outside of the column1 div and then it will no longer be restricted. 尝试将img标签放置在column1 div之外,然后将不再受到限制。

If the img is not showing ontop of the text try add css value z-index. 如果img不在文本上方显示,请尝试添加css值z-index。

img {
z-index:2;
}

use : 采用 :

$("#test" ).hover(function() {
  $("#test1").show();
},function(){
    $("#test1").hide();
});

https://jsfiddle.net/2rt836co/4/ https://jsfiddle.net/2rt836co/4/

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

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