打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
jQuery实现图片放大预览效果

jQuery实现图片放大预览效果

一个jQuery实现的相册图片预览效果,鼠标滑过图片,图片会自动放大显示,用于相册、幻灯片的大图预览非常不错。

调用代码非常简单:

JS:

1<script src="jquery.js" type="text/javascript"></script>
2<script src="main.js" type="text/javascript"></script>

HTML:

1<a href="1.jpg" class="preview"><img src="1s.jpg" alt="gallery thumbnail" /></a>

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 */
    $(".preview > img").hover(function(e){
        this.t = this.title;
        this.title = "";   
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='3.jpg' 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();
    });   
    $(".preview > img").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();
});

html:

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="JS代码,图片放大,图片预览,图片特效,jQuery特效,JS图片特效,JS广告代码,JS常用代码" />
<meta name="description" content="jQuery实现图片放大预览效果,更多图片放大,图片预览,图片特效,jQuery特效代码请访问脚本分享网JS代码频道。" />
<title>jQuery实现图片放大预览效果_脚本分享网</title>
<script src="jquery.js" type="text/javascript"></script>
<script src="main.js" type="text/javascript"></script>
</meta>


<style>
body {
    margin:0;
    padding:40px;
    background:#fff;
    font:80% Arial, Helvetica, sans-serif;
    color:#555;
    line-height:180%;
}

h1{
    font-size:180%;
    font-weight:normal;
    color:#555;
}
h2{
    clear:both;
    font-size:160%;
    font-weight:normal;
    color:#555;
    margin:0;
    padding:.5em 0;
}
a{
    text-decoration:none;
    color:#f30;   
}
p{
    clear:both;
    margin:0;
    padding:.5em 0;
}
pre{
    display:block;
    font:100% "Courier New", Courier, monospace;
    padding:10px;
    border:1px solid #bae2f0;
    background:#e3f4f9;   
    margin:.5em 0;
    overflow:auto;
    width:800px;
}

img{border:none;}
ul,li{
    margin:0;
    padding:0;
}
li{
    list-style:none;
    float:left;
    display:inline;
    margin-right:10px;
}



/*  */

#preview{
    position:absolute;
    border:1px solid #ccc;
    background:#333;
    padding:5px;
    display:none;
    color:#fff;
    }

/*  */
</style>
</head>
<body>

    <h1>Easy Image Preview with jQuery</h1>
   
    <h2>Image gallery  (without caption)</h2>

   
        <div class="preview"><img src="1s.jpg" alt="gallery thumbnail" /></div>
        <div class="preview"><img src="2s.jpg" alt="gallery thumbnail" /></div>
        <div class="preview"><img src="3s.jpg" alt="gallery thumbnail" /></div>
        <div class="preview"><img src="4s.jpg" alt="gallery thumbnail" /></div>
   

    <h2>Image gallery (with caption)</h2>

    <ul>
        <li><a href="1.jpg" class="preview" title="Lake and a mountain"><img src="1s.jpg" alt="gallery thumbnail" /></a></li>
        <li><a href="2.jpg" class="preview" title="Fly fishing"><img src="2s.jpg" alt="gallery thumbnail" /></a></li>
        <li><a href="3.jpg" class="preview" title="Autumn"><img src="3s.jpg" alt="gallery thumbnail" /></a></li>
        <li><a href="4.jpg" class="preview" title="Skiing on a mountain"><img src="4s.jpg" alt="gallery thumbnail" /></a></li>
    </ul><br />
<br />
<br />
<br />


</body>
</html>

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
6种方法来画一只狗
jQuery 图片切换,带标题和说明文字
jquery应用 图片拖拽排序
jquery图片滚动
jQuery---突出展示案例
jQuery——jQuery的CSS,class,属性操作及案例应用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服