以下是 jQuery点击图像居中放大插件Zoom特效代码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>jQuery点击图像居中放大插件Zoom</title>
<link rel="stylesheet" href="css/zzsc.css">
</head>
<body>
<div class="page-body">
<div style="width: 80px; height: 80px; margin-right: 10px; padding: 10px; float: left; background: green;">点我看看!!!</div>
<div style="width: 100px; height: 50px; position: absolute; right: 50px; top: 250px; background: red;">
点我看看!!!
<div style="width: 80px; height: 100px; position: relative; left: -50px; top: 200px; padding: 10px; margin-left: 50px; background: blue;">点我看看!!!</div>
</div>
</div>
<script src="js/zoom.js"></script>
<script>
document.querySelector( '.page-body' ).addEventListener( 'click', function( event ) {
event.preventDefault();
zoom.to({ element: event.target });
} );
</script>
</body>
</html>
JS代码(zoom.js):
/*! * zoom.js 0.3 * http://lab.hakim.se/zoom-js * MIT licensed * * Copyright (C) 2011-2014 Hakim El Hattab,http://hakim.se */
var zoom = (function(){
var TRANSITION_DURATION = 800;
// The current zoom level (scale)var level = 1;
// The current mouse position,used for panningvar mouseX = 0,mouseY = 0;
// Timeout before pan is activatedvar panEngageTimeout = -1,panUpdateInterval = -1;
// Timeout for callback functionvar callbackTimeout = -1;
// Check for transform support so that we can fallback otherwisevar supportsTransforms ='WebkitTransform' in document.body.style ||'MozTransform' in document.body.style ||'msTransform' in document.body.style ||'OTransform' in document.body.style ||'transform' in document.body.style;
if( supportsTransforms ){
// The easing that will be applied when we zoom in/outdocument.body.style.transition = 'transform '+ TRANSITION_DURATION +'ms ease';
document.body.style.OTransition = '-o-transform '+ TRANSITION_DURATION +'ms ease';
document.body.style.msTransition = '-ms-transform '+ TRANSITION_DURATION +'ms ease';
document.body.style.MozTransition = '-moz-transform '+ TRANSITION_DURATION +'ms ease';
document.body.style.WebkitTransition = '-webkit-transform '+ TRANSITION_DURATION +'ms ease';
}
// Zoom out if the user hits escapedocument.addEventListener( 'keyup',function( event ){
if( level !== 1 && event.keyCode === 27 ){
zoom.out();
}
}
);
// Monitor mouse movement for panningdocument.addEventListener( 'mousemove',function( event ){
if( level !== 1 ){
mouseX = event.clientX;
mouseY = event.clientY;
}
}
);
/** * Applies the CSS required to zoom in,prefers the use of CSS3 * transforms but falls back on zoom for IE. * * @param{
Object}
rect * @param{
Number}
scale */
function magnify( rect,scale ){
var scrollOffset = getScrollOffset();
// Ensure a width/height is setrect.width = rect.width || 1;
rect.height = rect.height || 1;
// Center the rect within the zoomed viewportrect.x -= ( window.innerWidth - ( rect.width * scale ) ) / 2;
rect.y -= ( window.innerHeight - ( rect.height * scale ) ) / 2;
if( supportsTransforms ){
// Resetif( scale === 1 ){
document.body.style.transform = '';
document.body.style.OTransform = '';
document.body.style.msTransform = '';
document.body.style.MozTransform = '';
document.body.style.WebkitTransform = '';
}
// Scaleelse{
var origin = scrollOffset.x +'px '+ scrollOffset.y +'px',transform = 'translate('+ -rect.x +'px,'+ -rect.y +'px) scale('+ scale +')';
document.body.style.transformOrigin = origin;
document.body.style.OTransformOrigin = origin;
document.body.style.msTransformOrigin = origin;
document.body.style.MozTransformOrigin = origin;
document.body.style.WebkitTransformOrigin = origin;
document.body.style.transform = transform;
document.body.style.OTransform = transform;
document.body.style.msTransform = transform;
document.body.style.MozTransform = transform;
document.body.style.WebkitTransform = transform;
}
}
else{
// Resetif( scale === 1 ){
document.body.style.position = '';
document.body.style.left = '';
document.body.style.top = '';
document.body.style.width = '';
document.body.style.height = '';
document.body.style.zoom = '';
}
// Scaleelse{
document.body.style.position = 'relative';
document.body.style.left = ( - ( scrollOffset.x + rect.x ) / scale ) + 'px';
document.body.style.top = ( - ( scrollOffset.y + rect.y ) / scale ) + 'px';
document.body.style.width = ( scale * 100 ) + '%';
document.body.style.height = ( scale * 100 ) + '%';
document.body.style.zoom = scale;
}
}
level = scale;
}
/** * Pan the document when the mouse cursor approaches the edges * of the window. */
function pan(){
var range = 0.12,rangeX = window.innerWidth * range,rangeY = window.innerHeight * range,scrollOffset = getScrollOffset();
// Upif( mouseY < rangeY ){
window.scroll( scrollOffset.x,scrollOffset.y - ( 1 - ( mouseY / rangeY ) ) * ( 14 / level ) );
}
// Downelse if( mouseY > window.innerHeight - rangeY ){
window.scroll( scrollOffset.x,scrollOffset.y + ( 1 - ( window.innerHeight - mouseY ) / rangeY ) * ( 14 / level ) );
}
// Leftif( mouseX < rangeX ){
window.scroll( scrollOffset.x - ( 1 - ( mouseX / rangeX ) ) * ( 14 / level ),scrollOffset.y );
}
// Rightelse if( mouseX > window.innerWidth - rangeX ){
window.scroll( scrollOffset.x + ( 1 - ( window.innerWidth - mouseX ) / rangeX ) * ( 14 / level ),scrollOffset.y );
}
}
function getScrollOffset(){
return{
x:window.scrollX !== undefined ? window.scrollX:window.pageXOffset,y:window.scrollY !== undefined ? window.scrollY:window.pageYOffset}
}
return{
/** * Zooms in on either a rectangle or HTML element. * * @param{
Object}
options * * (required) * - element:HTML element to zoom in on * OR * - x/y:coordinates in non-transformed space to zoom in on * - width/height:the portion of the screen to zoom in on * - scale:can be used instead of width/height to explicitly set scale * * (optional) * - callback:call back when zooming in ends * - padding:spacing around the zoomed in element */
to:function( options ){
// Due to an implementation limitation we can't zoom in// to another element without zooming out firstif( level !== 1 ){
zoom.out();
}
else{
options.x = options.x || 0;
options.y = options.y || 0;
// If an element is set,that takes precedenceif( !!options.element ){
// Space around the zoomed in element to leave on screenvar padding = typeof options.padding === 'number' ? options.padding:20;
var bounds = options.element.getBoundingClientRect();
options.x = bounds.left - padding;
options.y = bounds.top - padding;
options.width = bounds.width + ( padding * 2 );
options.height = bounds.height + ( padding * 2 );
}
// If width/height values are set,calculate scale from those valuesif( options.width !== undefined && options.height !== undefined ){
options.scale = Math.max( Math.min( window.innerWidth / options.width,window.innerHeight / options.height ),1 );
}
if( options.scale > 1 ){
options.x *= options.scale;
options.y *= options.scale;
options.x = Math.max( options.x,0 );
options.y = Math.max( options.y,0 );
magnify( options,options.scale );
if( options.pan !== false ){
// Wait with engaging panning as it may conflict with the// zoom transitionpanEngageTimeout = setTimeout( function(){
panUpdateInterval = setInterval( pan,1000 / 60 );
}
,TRANSITION_DURATION );
}
if( typeof options.callback === 'function' ){
callbackTimeout = setTimeout( options.callback,TRANSITION_DURATION );
}
}
}
}
,/** * Resets the document zoom state to its default. * * @param{
Object}
options * - callback:call back when zooming out ends */
out:function( options ){
clearTimeout( panEngageTimeout );
clearInterval( panUpdateInterval );
clearTimeout( callbackTimeout );
magnify({
x:0,y:0}
,1 );
if( options && typeof options.callback === 'function' ){
setTimeout( options.callback,TRANSITION_DURATION );
}
level = 1;
}
,// Aliasmagnify:function( options ){
this.to( options )}
,reset:function(){
this.out()}
,zoomLevel:function(){
return level;
}
}
}
)();
CSS代码(zzsc.css):
html,body{font-family:'Lato',Times,'Times New Roman',serif;font-size:16px;color:#eee;}
html{background:#323232;}
body{margin:0;}
a{color:cyan;}
em{font-style:italic;}
.page-header,.page-body{max-width:800px;margin:0 auto;padding:40px;}
.page-header{background:rgba(0,0,0,0.2);font-size:20px;line-height:1.4;}
.page-header div{margin-top:15px;}
.page-body{line-height:1.4;font-size:16px;}
.page-body p{margin-bottom:40px;}
.page-body h2{margin-bottom:5px;color:#eee;font-family:'Lato',sans-serif;line-height:0.9em;letter-spacing:0.02em;font-weight:bold;}