码迷,mamicode.com
首页 > Web开发 > 详细

jquery图片预览功能的效果

时间:2015-11-27 20:01:03      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

技术分享技术分享

效果如上:

index.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image Preview with jQuery</title>
<meta name="description" content="">
<script src="jquery.js" type="text/javascript"></script>
<script src="main.js" type="text/javascript"></script>
</meta>
<style>
#preview{
	position:absolute;
	border:1px solid #ccc;
	background:#333;
	padding:5px;
	display:none;
	color:#fff;
}
</style>
</head>
<body>
	<h2>Image gallery  (without caption)</h2>
		<a href="1.jpg" class="preview"><img src="1s.jpg"  /></a>
		<a href="2.jpg" class="preview"><img src="2s.jpg" /></a>
		<a href="3.jpg" class="preview"><img src="3s.jpg" /></a>
		<a href="4.jpg" class="preview"><img src="4s.jpg" /></a>
	<h2>Image gallery (with caption)</h2>
		<a href="1.jpg" class="preview" title="Lake and a mountain"><img src="1s.jpg"  /></a>
		<a href="2.jpg" class="preview" title="Fly fishing"><img src="2s.jpg"  /></a>
		<a href="3.jpg" class="preview" title="Autumn"><img src="3s.jpg" /></a>
		<a href="4.jpg" class="preview" title="Skiing on a mountain"><img src="4s.jpg" /></a>
	

</body>
</html>

  main.js

/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup‘s distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id=‘preview‘><img src=‘"+ this.href +"‘ alt=‘Image preview‘ />"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});

  目录结构:

技术分享

如1s.jpg 尺寸小的图片, 1.jpg是尺寸大的图片

jquery图片预览功能的效果

标签:

原文地址:http://www.cnblogs.com/nbkyzms/p/5001429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!