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

时间:2015-08-03 11:34:17

标签: javascript jquery html5 lightbox

            <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>

它会显示这是lightbox123456 !!!不管你点击什么。

请帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

事实上,如果您将.box的位置更改为relative,则可以看到两个框都显示出来。我创建了jsfiddle来显示效果。

由于您只希望它在锚点后显示方框,您可以执行以下操作:

  1. .backdrop移动到身体的最后一个,只保留一个,因为它不会与您点击的锚点相关。

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

  3. 为他们制作动画。

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

    &#13;
    &#13;
    $(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');
      });
    }
    &#13;
    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;
    }
    &#13;
    <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>
    &#13;
    &#13;
    &#13;