使用HTML / CSS将鼠标悬停在图像上的放大效果

无情的

仅使用html和CSS就能达到这种效果吗?在示例图像中,您可以看到鼠标悬停的放大的圆形区域(来自高分辨率源图片)。它不是圆形区域或没有边界。

鼠标悬停在HTML / CSS上的图像上所需的放大效果的示例

在此站点示例中,您可以看到一个很好的示例(但是它使用jquery,我不知道将此代码放在我的网页中的什么位置)

有人可以给我一个提示如何开始编写此代码的提示吗?(如果可行)。非常感谢!

哈维尔

要使用第二个示例很简单,只需复制html页面中的html,css页面中的css和html页面中的jquery代码,就像这样

<script type="text/javascript">$(document).ready(function(){

var native_width = 0;
var native_height = 0;

//Now the mousemove function
$(".magnify").mousemove(function(e){
    //When the user hovers on the image, the script will first calculate
    //the native dimensions if they don't exist. Only after the native dimensions
    //are available, the script will show the zoomed version.
    if(!native_width && !native_height)
    {
        //This will create a new image object with the same image as that in .small
        //We cannot directly get the dimensions from .small because of the 
        //width specified to 200px in the html. To get the actual dimensions we have
        //created this image object.
        var image_object = new Image();
        image_object.src = $(".small").attr("src");

        //This code is wrapped in the .load function which is important.
        //width and height of the object would return 0 if accessed before 
        //the image gets loaded.
        native_width = image_object.width;
        native_height = image_object.height;
    }
    else
    {
        //x/y coordinates of the mouse
        //This is the position of .magnify with respect to the document.
        var magnify_offset = $(this).offset();
        //We will deduct the positions of .magnify from the mouse positions with
        //respect to the document to get the mouse positions with respect to the 
        //container(.magnify)
        var mx = e.pageX - magnify_offset.left;
        var my = e.pageY - magnify_offset.top;

        //Finally the code to fade out the glass if the mouse is outside the container
        if(mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0)
        {
            $(".large").fadeIn(100);
        }
        else
        {
            $(".large").fadeOut(100);
        }
        if($(".large").is(":visible"))
        {
            //The background position of .large will be changed according to the position
            //of the mouse over the .small image. So we will get the ratio of the pixel
            //under the mouse pointer with respect to the image and use that to position the 
            //large image inside the magnifying glass
            var rx = Math.round(mx/$(".small").width()*native_width - $(".large").width()/2)*-1;
            var ry = Math.round(my/$(".small").height()*native_height - $(".large").height()/2)*-1;
            var bgp = rx + "px " + ry + "px";

            //Time to move the magnifying glass with the mouse
            var px = mx - $(".large").width()/2;
            var py = my - $(".large").height()/2;
            //Now the glass moves with the mouse
            //The logic is to deduct half of the glass's width and height from the 
            //mouse coordinates to place it with its center at the mouse coordinates

            //If you hover on the image now, you should see the magnifying glass in action
            $(".large").css({left: px, top: py, backgroundPosition: bgp});
        }
    }
})

}

在您的html页面之间和之间使用此代码

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将鼠标悬停在图像上时放大-CSS和HTML

使用CSS将鼠标悬停在图像上的图像

将鼠标悬停在带有html放大镜的图像上

使用 CSS/HTML + Transitions 将鼠标悬停在图像上时如何显示 div?

将鼠标悬停在图像上以显示文本框不起作用(html、css)

将鼠标悬停在图像上时,使用css显示菜单

如何使用CSS将鼠标悬停在文本上

将鼠标悬停在img上时,所有其他图像都会使用CSS:not(:hover)模糊

将鼠标悬停在图像上会更改HTML背景

使用Ajax将鼠标悬停在图像上后显示信息

使用 ng-repeat 将鼠标悬停在表格中的文本上时显示图像

仅将鼠标悬停在URL上之后才加载图像(使用DIV标签)

当我将鼠标悬停在按钮上时,HTML / CSS会闪烁

将鼠标悬停在HTML / CSS中的建筑地板上

将鼠标悬停在按钮上并更改其属性 html/js/css

使用StatusBar将鼠标悬停在按钮上

将鼠标悬停在使用JavaScript的元素上吗?

当使用 css 将鼠标悬停在按钮上时,如何更改按钮的文本?

如何使用CSS将鼠标悬停在整个列表元素(包括项目符号)上?

是否可以通过仅使用CSS和HTML将鼠标悬停在某个组件上而使其消失?

将鼠标悬停在图像上,没有触摸但可以使用,如何删除多余的隐藏图像?

将鼠标悬停在图像上时,如何输出按钮和文本?我需要使用JQuery吗?

将鼠标悬停在图像上时,如何使用户的光标跳至页面上的特定坐标?

HTML CSS:带有子菜单的固定菜单,将鼠标悬停在菜单上之前会出现子菜单

将鼠标悬停在 HTML 和 CSS 中的按钮上时,避免其他按钮受到影响

使用 CSS,当用户将鼠标悬停在元素上时,如何为元素添加圆角灰色背景

我使用CSS将鼠标悬停在按钮上时,试图使按钮中的文本更改颜色

当使用CSS将鼠标悬停在按钮上时,如何更改网站的背景图片?

仅使用CSS将鼠标悬停在另一个div上时显示div