[错误]HTML5、Jquery - LightBox 仅显示最新信息

[Error]HTML5, Jquery - LightBox only shows latest information

            <html>
                <head>
                    <title>Quick Simple Light Box</title>
                    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
                    <style type="text/css">

                    body
                    {
                        font-family: Helvetica, Arial;
                    }

                    .backdrop
                    {
                        position:absolute;
                        top:0px;
                        left:0px;
                        width:100%;
                        height:100%;
                        background:#000;
                        opacity: .0;
                        filter:alpha(opacity=0);
                        z-index:50;
                        display:none;
                    }


                    .box
                    {
                        position:absolute;
                        top:20%;
                        left:30%;
                        width:500px;
                        height:300px;
                        background:#ffffff;
                        z-index:51;
                        padding:10px;
                        -webkit-border-radius: 5px;
                        -moz-border-radius: 5px;
                        border-radius: 5px;
                        -moz-box-shadow:0px 0px 5px #444444;
                        -webkit-box-shadow:0px 0px 5px #444444;
                        box-shadow:0px 0px 5px #444444;
                        display:none;
                    }

                    .close
                    {
                        float:right;
                        margin-right:6px;
                        cursor:pointer;
                    }

                    </style>

                    <script type="text/javascript">

                        $(document).ready(function(){

                            $('.lightbox').click(function(){
                                $('.backdrop, .box').animate({'opacity':'.50'}, 300, 'linear');
                                $('.box').animate({'opacity':'1.00'}, 300, 'linear');
                                $('.backdrop, .box').css('display', 'block');
                            });

                            $('.close').click(function(){
                                close_box();
                            });

                            $('.backdrop').click(function(){
                                close_box();
                            });

                        });

                        function close_box()
                        {
                            $('.backdrop, .box').animate({'opacity':'0'}, 300, 'linear', function(){
                                $('.backdrop, .box').css('display', 'none');
                            });
                        }

                    </script>

                </head>
                <body>

                <h1>This is my webpage...</h1>
                <a href="#" class="lightbox">Open Lightbox</a>

                <div class="backdrop"></div>
                <div class="box"><div class="close">x</div>This is the lightbox!!!</div>

                </body>
            </html>

您好,上面的灯箱代码有问题。

它显然只显示最新的信息,例如。

                <a href="#" class="lightbox">Open Lightbox</a>

                <div class="backdrop"></div>
                <div class="box"><div class="close">x</div>This is the lightbox!!!</div>




                <a href="#" class="lightbox">Open Lightbox</a>

                <div class="backdrop"></div>
                <div class="box"><div class="close">x</div>This is the lightbox123456!!!</div>

会显示This is the lightbox123456!!!无论您点击什么。

请帮忙,谢谢。

其实这两个都显示了,如果你把.box的位置改成relative,你可以看到两个框都出现了。我创建了一个 jsfiddle 来显示效果。

因为你只希望它在锚点之后显示框,你可以这样做:

  1. .backdrop移动到正文的最后,只保留一个,因为它不会与您单击的锚点一起。

  2. 直接在锚点后使用.next()到select.box,然后使用.add添加.backdrop组.

  3. 为它们制作动画。

如果 .backdrop 因您单击的锚点而异,我建议将 .box.backdrop 都包装到容器中,然后使用 $(this).next('.container').find('.box, .backdrop') 获取目标。或者也将锚点包裹在容器中,然后使用 $(this).siblings('.box, .backdrop') 到 select.

$(document).ready(function() {

  $('.lightbox').click(function() {
    // Get the target box, and the only backdrop
    
    // Because they will apply different opacity, we have to get them separately first.
    var $box = $(this).next('.box');
    var $backdrop = $('.backdrop');
    
    // Group them.
    var $targets = $box.add($backdrop);
    
    // Ensure the targets's display.
    $targets.css('display', 'block');
    
    // Separate the animation of each element if you want different parameters. or chain them.
    $backdrop.animate({
      'opacity': '.50'
    }, 300, 'linear');
    $box.animate({
      'opacity': '1.00'
    }, 300, 'linear');
    $targets.css('display', 'block');
  });

  $('.close').click(function() {
    close_box();
  });

  $('.backdrop').click(function() {
    close_box();
  });

});

function close_box() {
  $('.backdrop, .box').animate({
    'opacity': '0'
  }, 300, 'linear', function() {
    $('.backdrop, .box').css('display', 'none');
  });
}
body {
  font-family: Helvetica, Arial;
}
.backdrop {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  background: #000;
  opacity: .0;
  filter: alpha(opacity=0);
  z-index: 50;
  display: none;
}
.box {
  position: absolute;
  top: 20%;
  left: 30%;
  width: 500px;
  height: 300px;
  background: #ffffff;
  z-index: 51;
  padding: 10px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  -moz-box-shadow: 0px 0px 5px #444444;
  -webkit-box-shadow: 0px 0px 5px #444444;
  box-shadow: 0px 0px 5px #444444;
  display: none;
}
.close {
  float: right;
  margin-right: 6px;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<a href="#" class="lightbox">Open Lightbox</a>


<div class="box">
  <div class="close">x</div>This is the lightbox!!!
</div>
 
<a href="#" class="lightbox">Open Lightbox</a>
<div class="box">
  <div class="close">x</div>This is the lightbox123456!!!</div>

<div class="backdrop"></div>