2013-10-16 58 views
0

我想建立一個使用jQuery的模態插件。如果我只需要調用一個模態,但是當頁面中有更多的模態時,它就可以正常工作,無論是模態的調用還是有時只有一次,如果嘗試更改代碼。我想定位點擊時具有相同ID的模式。不知道代碼有什麼問題,幫助將非常感激。代碼如下。Jquery自定義模式插件

您可以查看演示http://jsfiddle.net/A9AtV/

下面是代碼

CSS

body { 
    margin:0; 
    font-family:'Droid Sans', sans-serif; 
    font-size:13px; 
    line-height:20px; 
    color:#333; 
} 
h1 { 
    font-size:40px; 
    color:#dd7e2a; 
} 
a { 
    text-decoration:none; 
} 
form, button { 
    font-family:'Droid Sans', sans-serif; 
} 
article { 
    padding:10px; 
} 
.modal { 
    background:#fff; 
    border:3px solid #dd7e2a; 
    width:600px; 
    padding:10px; 
    position:absolute; 
    display:none; 
    z-index:2; 
} 
.controls { 
    width:100%; 
    margin:0 0 20px 0; 
} 
.controls label { 
    width:100%; 
    display:block; 
} 
.close { 
    position:absolute; 
    right:-10px; 
    top:-10px; 
    background:#fff; 
    padding:3px 10px; 
    border-radius:50%; 
    text-align:center; 
    border:3px solid #dd7e2a; 
    color:#333; 
    font-size:bold; 
    cursor:pointer; 
} 
.backdrop { 
    width:100%; 
    height:100%; 
    position:fixed; 
    background:rgba(47, 175, 102, 0.8); 
    top:0; 
    left:0; 
    z-index:1; 
} 
form button, .contact { 
    text-align:center; 
    margin:0 auto; 
    display:block; 
    background:#fff; 
    outline:none; 
    border:3px solid #dd7e2a; 
    line-height:30px; 
    cursor:pointer; 
    padding:0 10px; 
    transition:all 1s ease; 
    color:#dd7e2a; 
    max-width:100px; 
} 
form button:hover, .contact:hover { 
    border-color:rgba(47, 175, 102, 0.8); 
    color:rgba(47, 175, 102, 0.8); 
} 
form { 
    padding:20px; 
} 
body form textarea { 
    height:50px; 
} 
body form button { 
    margin:0; 
} 
form input, form textarea { 
    border:2px solid #dd7e2a; 
    height:25px; 
    width:300px; 
    transition:all 0.6s ease; 
    color:#dd7e2a; 
    outline:none; 
} 
form input:focus, form textarea:focus { 
    border-color:rgba(47, 175, 102, 0.8); 
} 

HTML

<div id="contactme" data-width="400" data-toggle="modal" class="modal"> 
    <form> 
     <div class="controls"> 
      <label>Name</label> 
      <input type="text" name="name"> 
     </div> 
     <div class="controls"> 
      <label>Email</label> 
      <input type="email" name="email"> 
     </div> 
     <div class="controls"> 
      <label>Message</label> 
      <textarea name="msg"></textarea> 
     </div> 
     <div class="controls"> 
      <button type="submit">Submit</button> 
     </div> 
    </form> 
</div> 
<div id="contactyou" data-width="400" data-toggle="modal" class="modal"> 
    <form> 
     <div class="controls"> 
      <label>Name</label> 
      <input type="text" name="name"> 
     </div> 
     <div class="controls"> 
      <button type="submit">Submit</button> 
     </div> 
    </form> 
</div> 
<a href="#" data-target="contactme" class="contact">First Button</a><br> 
<a href="#" data-target="contactyou" class="contact">Second Button</a> 

的JavaScript

(function() { 
    var someglobal = $('.modal'); 

    // Modal Functinonality 
    var contactform = { 
     container: $(someglobal), 
     config: { 
      effect: 'slideToggle', 
      speed: 500, 
     }, 
     // Initial Function 
     init: function (config) { 
      $.extend(this.config, config); 
      var id = $('.modal').attr('id'); 
      var newv = $('a').data('target'); 

      sv = $('a').data('target'); 
      $('a[data-target]').on('click', this.show); 

      function modal() { 
       var wid = $('div').data('width'); 
       cont = $(someglobal) 
       wid = $(cont).css({ 
        'width': wid + 'px', 
       }); 

       wid = $(cont).css({ 
        'left': ($(window).width() - $(cont).width())/2 + 'px', 
         'top': ($(window).height() - $(cont).height())/2 + 'px' 
       }); 
      } 

      // Adding modal on resize and dom ready 
      $(document).ready(modal); 
      $(window).resize(modal); 

      console.log(someglobal.attr('id')); 
     }, 
     // Show Function 
     show: function() { 
      var cf = contactform, 
       //container = cf.container, 
       config = cf.config 
       $new = $(this).data('target'), 
       console.log(someglobal); 

      if (sv == $(someglobal).attr("id")) { 
       cf.close.call(someglobal); 
       cf.fadediv.call(someglobal); 
       someglobal[config.effect](config.speed); 
      } 
     }, 

     // Close function 
     close: function() { 
      var $this = $(this);    
      if ($this.find('span.close').length) return; 

      $('<span class=close>x</span>') 
       .prependTo(this) 
       .on('click', function() { 
       $this[contactform.config.effect](contactform.config.speed); 
       $('body').find('div.backdrop').fadeOut(300); 
      }) 
     }, 
     // Backdrop Fade div  
     fadediv: function() { 
      $('<div></div>', { 
       class: 'backdrop' 
      }) 
       .appendTo('body'); 
     } 
    }; 

    // Custom overide for sure 
    contactform.init({ 
     effect: 'fadeToggle', 
     speed: 300 
    }); 
})(); 
+0

很抱歉,莫代爾類.. – LegendaryAks

回答

0

正如我所見,你總是在someglobal上運行,這是DOM中所有模態的列表。相反,您應該只對數據目標選擇的模式進行操作,並使用模式的ID。

你可能會考慮的另一件事是重構你的插件一點點。通常你會得到一個選擇器作爲參數,然後你對該選擇器所選擇的所有元素進行循環。

(function($){ 
    $.fn.yourmodal=function(selector,options){ 
     this.each(function(i, target){ 
      // and here you do your job for target 
     }); 
    } 
})(); 

你的情況,這樣你會增加這樣的事情到您的網頁:喜歡的東西

$(document).ready(function(){ 
    $.yourmodal(".modal"); 
}); 

和你的插件將更加普遍,可重複使用。

+0

感謝您的更新,但你能告訴我演示這樣我就可以更好地理解它。 – LegendaryAks

+0

http://neswork.com/jquery/ball/和http://neswork.com/jquery/ball/jquery.ball.js並採取行動:http://neswork.com/flickr/rollr/ – Gavriel

0

您需要爲模型div提供唯一的類名稱。 HTML

<div id="contactme" data-width="400" data-toggle="modal" class="modal1"> 
... 

<div id="contactyou" data-width="400" data-toggle="modal" class="modal2"> 
... 

而在Javascript中傳遞你需要在

var someglobal = $('.modal1'); // or model2