2014-03-02 99 views
0

我想知道是否有方法通过移动鼠标来探索div的内容?比如像1000px * 1000px图片500px * 500px div内容overflow:hidden并且能够通过将光标放在div的右下角看到图片的其余部分。通过移动鼠标探索div内容

如果有办法我应该怎么做?

+0

对于这样的代码的请求实际上不是一个问题,是不适合于堆栈溢出。我建议你看看这个 - [如何提问](http://stackoverflow.com/questions/how-to-ask)** - 当你有一个特定的与编程相关的问题时回头看看。 – pes502

+0

更好地描述图像应该如何表现。它应该滚动还是什么? – dfsq

+0

嗯,我不是要求一个代码,只是我应该继续下去的方式,就像我应该使用格子上方的格子以不同的速度向上/向下/向左/向下移动,但是如果我这样做,听起来像我一样为了这么做需要做大量的网格,所以也许还有另一种“方式”。我当然不希望人们为我做这项工作,只是为了给我一个提示。我想学习不要被扛走,对不起,如果我问我的问题的方式导致这种误解,我只是不知道该怎么去问,因为我真的不知道我想问什么(即它是“探索“一个div,我真的不知道它是否是正确的热敏) –

回答

1

LIVE DEMO

一件好事顺畅?

<div id="mmGal"> 
    <img id="mmImg" src="image.jpg"> 
</div> 

CSS:

#mmGal{ 
    position:relative; 
    top:60px; 
    margin:0 auto; 
    width:500px; 
    height:220px; 
    overflow:hidden; 
    background:#eee; 
} 

JQ:

$(function(){ 

    var $mmGal = $('#mmGal'), 
     $mmImg = $('#mmImg'), 
     damp = 10, // higher number = smoother response 
     X = 0, Y = 0, mX = 0, mY = 0, wDiff, hDiff, zeno; 

    function motion(){ 
    zeno = setInterval(function(){ // Zeno's paradox "catching delay" 
     X += (mX-X)/damp; 
     Y += (mY-Y)/damp;   
     $mmGal.scrollLeft(X*wDiff).scrollTop(Y*hDiff); 
    }, 26); 
    } 

    // Get image size after it's loaded 
    $mmImg.one('load', function() { 
    wDiff = (this.width/$mmGal.width())-1,   
    hDiff = (this.height/$mmGal.height())-1; 
    }).each(function() { 
    if(this.complete) $(this).load(); 
    }); 

    $mmGal.mousemove(function(e) { 
    mX = e.pageX-this.offsetLeft; 
    mY = e.pageY-this.offsetTop; 
    }).hover(function(e){ 
    // Start moving on mouseenter and stop intv. on mouseleave after 1200ms 
    return e.type=='mouseenter'? motion() : setTimeout(function(){ 
     clearInterval(zeno); 
    },1200); // clear if not used 
    }); 

}); 

http://en.wikipedia.org/wiki/Zeno%27s_paradoxes

+0

哇,这正是我正在寻找的,非常令人印象深刻,清楚明白,非常感谢! –

+0

@BrennanSei :)很高兴我能帮忙!快乐的编码 –

0

给予widht和高度DIV包裹的图像

这里是DEMO

:hover添加overflow: visible;到div

0

这几乎是你想要的。看到这个小提琴http://jsfiddle.net/sajith/RM9wK/

HTML

<div id="container"><img src="http://farm4.staticflickr.com/3668/12858161173_8daa0b7e54_b.jpg"/></div> 

CSS

#container { 
    width:300px; 
    height:300px; 
    overflow: hidden; 
} 
#container img { 
    position: relative; 
} 

的Javascript

$(function() { 
    $("#container").mousemove(function(event) { 

     var width = $("#container img").width(); 
     var height = $("#container img").height(); 
     var divWidth = $("#container").width(); 
     var divHeight = $("#container").height(); 

     var xPos = (width/divWidth - 1) * event.pageX 
     var yPos = (height/divHeight -1) * event.pageY 

     $("#container img").css('left', '-'+ xPos+'px'); 
     $("#container img").css('top', '-'+ yPos+'px'); 

    }); 
}); 
+0

在每一个鼠标移动 - 强制jQuery再次查找文件再次为相同的选择器是一个非常糟糕的主意 –

+0

多数民众赞成在什么提问者 – SajithNair

+0

dom查找是不是真的需要,如果他可以硬编码的宽度和高度 – SajithNair

-1

我会用“触发器”(热点)〜添加一些小的div元素,并设置自己的位置,只要你想,现在当鼠标进入触发一些事件....

简单的例子:jsfiddle

CSS

div.container { 
    position:relative; 
    width:100px; 
    height:100px; 
    overflow:hidden; 
} 
.trigger { 
    right:0; 
    bottom:0; 
    position:absolute; 
    z-index:2; 
    width:10px; 
    height:10px; 
    background-color:transparent; 
} 

HTML

<div class='container'> 
    <img src='http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg' /> 
    <div class='trigger'></div> 
</div> 

jQuery的

$('.trigger').mouseenter(
    function(){ 
    $(this).parent('.container').css({ 
     'width':'220px', 
     'height':'250px' 
    }); 
    }); 
$('.container').mouseleave(
    function(){ 
    $(this).css({ 
     'width':'100px', 
     'height':'100px' 
    }); 
}); 
+0

现在的问题是如何探索图像的所有其他部分,同时**将鼠标移动到包含区域上,如果您用鼠标触及右下角,则可以在对角线上探索大部分图像。另外在jQuery中,不需要设置':'220px''就是':200'。 jQuery在这方面非常聪明。另外关于更好的用户界面和用户体验,应该避免在页面周围闪现内容。 –