2014-06-08 40 views
0

我有這個代碼,當任何複選框被選中時自動增加div高度,當未選中時降低高度,但是我想在屏幕上垂直居中時增加或減少div身高,我該怎麼辦?具有自動增量功能的中心div(高度)

感謝您的幫助!

http://jsfiddle.net/MetCastle/6adby/

$(function() { 
     $(':checkbox').click(function() { 

     $(".scroll").show(); 

     var delta = $(this).is(':checked') ? -1 : 1; 

     if(delta < 0){ 
     $("#scroll").append("<h2>" + $(this).next("label").html() + "</h2>"); 
     $('.count').text(parseInt($('.count').text()) - 1); 
     } 
     else{ 
     $("#scroll h2:contains('" + $(this).next("label").html() + "')").remove(); 
     $('.count').text(parseInt($('.count').text()) + 1); 
     } 

     if ($(".count").text() == "0") { 
     var children = $('.options').children('input:not(:checked)'); 
     for(var i= 0; i<children.length; i++) { 
      $(children[i]).prop('disabled', true); 
     } 
     } else { 
     var children = $('.options').children('input:not(:checked)'); 
     for(var i= 0; i<children.length; i++) { 
      $(children[i]).prop('disabled', false); 
     } 
     } 

     if ($(".count").text() == "18") { 
     $(".scroll").hide(); 
     } 

    }); 
    }); 

回答

2

您可以設置到500%,然後在每個另外重新計算邊距是-height/2

top:50% 

,然後在你的方法設置

var scroll=$('#scroll'); 
scroll.css('margin-top',(Math.ceil(scroll.height()/-2)+'px')) 

http://jsfiddle.net/6adby/14/

+0

這正是我需要的,非常感謝! –