1

.我正在尝试制作一些画廊。想要使用 addClass 来显示图像,然后单击同一个地方以删除这个我在谷歌上搜索帮助的新类,但是在尝试让它正常工作一个小时后,我正在向你寻求帮助。我在这里找到了反向选项的解决方案(首先删除并添加)

代码:

<html>
 <script type="text/javascript" src="http://www.s-ola.me/js/jquery.js"></script>
 <script type="text/javascript" src="http://www.s-ola.me/js/jquery.nailthumb.1.1.min.js"></script>
<style>

#window {
width:              570px;
height:             455px;
position:           relative;
background-color:   grey;
padding:            20px;
margin:             0 auto;
top:                50%;
margin-top:         -225px;
}

.image {
display:    inline-block;
margin:     5px;
}

 #wrapper {
width:              568px;
height:             455px;
position:           absolute;
}

.close {
display:    none;
}
.big_image{
width:      100%;
display:    block;
}
</style>
<body>

<div id="window">
<div id="wrapper">
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
    <img class="image" src="http://www.livethelesson.com/wp-content/uploads/2012/01/snow.jpg" />
</div>
</div>

<script>

$(document).ready(function(){
$('.image').nailthumb({containerClass:'image',height:100, width:100, fitDirection:'center', proportions:0.5, maxShrink:0.5});
})

$(document).ready(function(){

$('img').click(function(){
    $('.image').toggleClass('close');
    $('#wrapper').append('<img src="'+this.src+'" class="big_image" />');
})

})
$(document).ready(function(){
$('.big_image').click(function(){
    if($('.big_image')) {$('.big_image').remove(); $('.image').toggleClass('close'); }
})

})

</script>
</body>
</html>

请让我知道我在哪里弄错了。

再次感谢。阿列克谢 alexela.biz


更新

4

3 回答 3

2

试试这个:

$('#wrapper').toggleClass('close');

这将在它不存在时添加一个关闭类,并在它存在时删除该类

于 2012-12-11T14:40:11.543 回答
2

终于成功了这是我的解决方案:

$(document).ready(function(){
$('img.image, img.big_image').live('click', function(){
    if (!$('.big_image').length) {
        $('.image').addClass('close');
        $('#wrapper').append('<img class="big_image" src="'+this.src+'" />');
    }
    else
    {
        $('.image').removeClass('close');
        $('.big_image').remove();
    }
})
})

希望它会有所帮助

于 2012-12-11T17:48:18.007 回答
1
 if($('#wrapper').hasClass('close')) {$('#wrapper').removeClass('class')}

你可能的意思是:

 if($('#wrapper').hasClass('close')) {$('#wrapper').removeClass('close')}
于 2012-12-11T14:37:53.543 回答