最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - html zoom on mouse over - Stack Overflow

programmeradmin3浏览0评论

I want to have a photo in a site, and when I mouse over, I want a little magnifier zoom effect.

Can I do this in html with canvas or something? javascript maybe?

thank you

I want to have a photo in a site, and when I mouse over, I want a little magnifier zoom effect.

Can I do this in html with canvas or something? javascript maybe?

thank you

Share Improve this question asked Feb 9, 2011 at 1:23 pvinispvinis 4,1395 gold badges40 silver badges59 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Enclose your photo in a div and add Zoom via CSS on hover. You may want to increase the z-index upon hover. You can add to the below CSS to make the zoomed photo look/style better. If you don't want to reinvent the wheel, look out for some Jquery plugin which may acplish the same thing in an elegant manner with less effort.

CSS:

#div-1 {
            position: absolute;
        }
#div-1.hover {
            position: absolute; zoom: 70%; border: solid 1px; z-index:10;
        }

Jquery/Javascript:

          <script type = "text/javascript"> 
$(document).ready(function() {
$(".div-1").onmouseover(function() {
    toggle_visibility('div-1');
})

function toggle_visibility(id) {
    var e = document.getElementById(id);
    if ($(e).hasClass("hover")) {
        $(e).removeClass("hover");
    } else {
        $($(e)).addClass("hover");
        $($(e)).attr({
            width: "100%",
            height: "100%"
        });
    }
}}); < /script>

Canvas isn't supported by IE without third-party plug-ins. You'll want to do this in JavaScript. jQuery makes this very easy and clean. Bind handlers for the hover in / out events for the image element you want to zoom using .hover(). "Binding handlers" simply means you pass two functions to .hover() which will be executed when the user hovers in and out, respectively. These functions will use animate(), which you can simply pass a new size.

Have a look at the documentation for .animate() and .hover(). If you're totally new to jQuery, check out the tutorials.

You can show the image in a div as a background-image and change the position with a little javascript.

Here's a library with some examples: http://www.mind-projects.it/projects/jqzoom/demos.php

发布评论

评论列表(0)

  1. 暂无评论