Jquery在jQuery错误中放大缩略图

时间:2013-04-30 20:16:34

标签: javascript jquery

我正在尝试复制一个jquery插件的示例,该插件可以放大桌面缩略图中的图像,问题是,每当我点击它时图像都不会从缩略图中放大

以下是jquery演示的原始来源,它放大了我想要复制的缩略图的图像 How to enlarge image from thumbnail in jQuery?

这是他的工作演示http://jsbin.com/egevij/3/edit

问题出在我的HTML中。有人可以指出我哪里出错吗?

这是我的HTML

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<link rel="stylesheet" href="css/forms.css">
<meta charset=utf-8 />
<title>Demo by roXon</title>
</head>
<body>


<div id="jQ_popup_window">
<div id="jQ_popup" class="shadow radius">
    <div id="jQ_popup_close"></div> 
<script type = "text/javascript" src ="trouble.js"></script>
</div>
</div>




<img src="http://placehold.it/250x150/cf5" data-full="http://placehold.it/860x590/cf5" alt="" />

<img src="http://placehold.it/250x150/fof" data-full="http://placehold.it/860x590/fof" alt="" /> 

</body>
</html>

forms.css CSS

CSS:

/* === POPUP WINDOW === */
#jQ_popup_window{
    background: rgba(0,0,0,0.6);
    left: 0;
    margin-left: -9000px;
    position: absolute;
    top: 0;
    width: 100%;
    z-index:999999;
}
#jQ_popup {
    background: #000;
    border: 1px solid #BDB9B8;
    margin: 30px auto;
    padding: 25px;
    position: relative;
    width: 600px; /* SET HERE DESIRED W .*/
}
#jQ_popup_close {
    background:#fff;
    cursor: pointer;
    height: 28px;
    width: 28px;
    position: absolute;
    z-index:999999;
    right: 10px;
    top: 10px;
    -webkit-border-radius:30px;
            border-radius:30px;
    border:2px solid #fff;
    border-color: rgba(255,255,255,0.2);
}
#jQ_popup_close:hover{
    background:#f00;    
}
/* #POPUP WINDOW */

jQuery的:

   // POPUP WINDOW:
   var scrT = $(window).scrollTop();
   $(window).scroll(function(){
      scrT = $(window).scrollTop(); 
   });

   // GET and use WINDOW HEIGHT //
    $.getDocHeight = function(){
        var D = document;
        return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
    };  


    // POPUP WINDOW (lightbox for video and other)  
    // GET WINDOW SCROLLtop OFFSET
$('[data-full]').on('click', function(e){

    e.preventDefault();

    $('#jQ_popup').css({
        top: scrT+15
    }).find('img').remove();
    $('#jQ_popup_window').height($.getDocHeight).fadeTo(0,0).css({
        marginLeft:0
    }).fadeTo(600,1);

    var imgToLoad = $(this).data('full');
  $('<img>', {src:imgToLoad, width:'100%'}).appendTo('#jQ_popup');


});
// close popup
$('#jQ_popup_close, #jQ_popup_window').on('click', function(){      
    $('#jQ_popup_window').fadeTo(600,0,function(){
        $(this).hide();         
    });
});
$('#jQ_popup').on('click', function(ev){
    ev.stopPropagation();
});
// end POPUP WINDOW

1 个答案:

答案 0 :(得分:1)

您的问题的解决方案只是移动JavaScript文件的导入。它应放在两个<img>标记之后的末尾。

<img src="http://placehold.it/250x150/cf5" data-full="http://placehold.it/860x590/cf5" alt="" />

<img src="http://placehold.it/250x150/fof" data-full="http://placehold.it/860x590/fof" alt="" /> 
<script type = "text/javascript" src ="trouble.js"></script>

标准做法是将javascript文件加载到头部最后或最后加载到正文中。将脚本标记放在其他html标记(例如<div>)中并不正常,但我以前从未见过它会产生这样的不良影响。