js确定响应元素宽度

时间:2016-07-26 15:39:53

标签: javascript function responsive

我是js的新手并且已经打了一个砖墙,改变了我根据屏幕尺寸计算元素宽度的方法。我开始只计算宽度是根据多少列表项设置为" plan-visible"。它在笔记本电脑屏幕上运行良好,但在屏幕较小时不会在css媒体查询宽度上中断。然后,我有了一个很好的想法(哈哈)在javascript中使用if / else语句直接尝试这个,我很少有经验。结果是它什么都没做。我研究和研究它看起来(对我来说)它应该工作,但我显然缺少一些东西。我已经包含了html,用于静态元素的原始js,然后我尝试使其响应失败。谢谢!

HTML:

<div id="product-table" class="clear">
    <div class="plan-visible">
      <h3>BALANCE<em>aap</em><img class="image aap" src="http://localhost:1400/images/baap.png"></h3>
        <a class="sbutton aap" href="">Go to AAP</a>         
        <ul>
            <li>Point and click your way through the preparation of your organization’s Affirmative Action plan.<br>&nbsp;</li>         
        </ul> 
    </div>
    <div class="plan-visible">
      <h3>BALANCE<em>trak</em><img class="image trak" src="http://localhost:1400/images/btrak.png"></h3>
        <a class="sbutton trak" href="">Learn More</a>        
        <ul>
            <li>Manage your organization’s hiring process from start to finish.<br>&nbsp;<br>&nbsp;</li>
        </ul>    
    </div>
    <div class="plan-visible">
        <h3>BALANCE<em>hub</em><img class="image hub" src="http://localhost:1400/images/bhub.png"></h3>
        <a class="sbutton hub" href="">Go to HUB</a>
        <ul>
            <li>Access a centralized compliance information center to view, publish, and share information from your BALANCEworks applications.</li>            
        </ul>
    </div>
    <div class="plan-visible">
        <h3>BALANCE<em>pay</em><img class="image pay" src="http://localhost:1400/images/bpay.png"></h3>
        <a class="sbutton" href="">Go to PAY</a>        
        <ul>
            <li>Conduct detailed compensation analyses, identify potential pay discrimination, and design a compliant pay structure</li>            
        </ul>
    </div>  
</div>

原始js:

$(document).ready(function() {
var width = 0;
$('.plan-visible').each(function() {
    width += $(this).outerWidth( true );
});
$('#product-table').css('width', width);
});

新的,破碎的js:

$(document).ready(function() {  
var mq = window.matchMedia( "(max-width: 500px)" ); 
if (mq.matches) {
  $('#product-table').css('446', width);
} else {
  var width = 0;
  $('.plan-visible').each(function() {
    width += $(this).outerWidth( true );
  });
  $('#product-table').css('width', width);
}
});

的CSS:

#product-table {
  margin: 100px auto;
    text-align: center;
  .plan-visible {
      font: 12px 'Lucida Sans', 'trebuchet MS', Arial, Helvetica;
      text-shadow: 0 1px rgba(255,255,255,.8);        
      background: #fff;      
      border: 1px solid #ddd;
      color: #333;
      padding: 20px; 
      float: left;
      position: relative;
      -moz-border-radius: 5px ;
    -webkit-border-radius: 5px;
      border-radius: 5px ;   }
  .plan-visible:nth-child(1) {width: 180px;}
  .plan-visible:nth-child(2) {width: 180px;}
  .plan-visible:nth-child(3) {width: 180px;}
  .plan-visible:nth-child(4) {width: 180px;}
  .plan-hidden {display: none;}
}


/* --------------- */   

#product-table h3 {
    font-size: 20px;
  color: $mainBlue;
    font-weight: normal;
    padding: 20px;
    margin: -20px -20px 50px -20px;
    background-color: $lightGrey;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;    
}

/* product image settings */

#product-table .plan-visible .image {
    display: block;
    height: 100px;
    width: 100px;
    margin: 15px auto -65px;
    -moz-border-radius: 100px;
    -webkit-border-radius: 100px;
    border-radius: 100px;
}

#product-table .plan-visible .image.aap {
    border: 3px solid $aap;
}

#product-table .plan-visible .image.trak {
    border: 3px solid $trak;
}

#product-table .plan-visible .image.hub {
    border: 3px solid $hub;
}

#product-table .plan-visible .image.pay {
    border: 3px solid $pay;
}

/* --------------- */

#product-table ul {
    margin: 20px 0 0 0;
  padding: 0;
    list-style: none;
}

#product-table li {
    border-top: 1px solid $lightGrey;
    padding: 20px 0 10px 0;
}

/* --------------- */

/* product table specific buttons */

.sbutton {
    position: relative;
    font-family: 'Roboto', Arial, sans-serif;
      padding: 8px 20px;
    margin: 20px 0 0 0;
    display: inline-block;
    background-color: $mainBlue; 
    color: white; 
    border-radius: 4px;
    border: 1px solid $mainBlue;
    text-align: center;
    text-decoration: none;
    font-size: 14px;
    -webkit-transition-duration: 0.4s; /* Safari */
    transition-duration: 0.4s;
    cursor: pointer;

}

.sbutton:hover {
    background-color: #94ae3c;
    color: #fff;
    border: 1px solid #94ae3c;
    text-decoration: none;
}

.sbutton.aap:hover {
    background-color: #6f2784;
    border: 1px solid #6f2784;
}

.sbutton.trak:hover {
    background-color: #5c89b4;
    border: 1px solid #5c89b4;
}

.sbutton.hub:hover {
    background-color: #b58f2e;
    border: 1px solid #b58f2e;  
}

.sbutton.pay:hover {
    background-color: #94ae3c;
    border: 1px solid #94ae3c;
}

0 个答案:

没有答案