2019
09-17

用JS自动获取宽度并赋值给图片

在网站开发的过程中,有时候为了做适应所有的终端设备,让图片根据终端的大小而改变。我们可以使用JS来实现。

 
          <!--产品列表-->
      <section class="yd_wap_news3 w_wap_news2">
        <h2>
          <i>
          </i>
          <em>
            <a href="/qiudong/" title="产品展示">更多</a></em>产品展示</h2>
        <ul>
        
          <li>
            <a href="/jack/52.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" >
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/51.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" >
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/50.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" >
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/49.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" >
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/48.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" >
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/47.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" >
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/46.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" >
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/45.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" >
              <span>测试产品</span></a>
          </li>
        
         
        </ul>
     
          <!--产品列表-->

比如说这样一组产品列表。


我们给他加这么一段JS,大概意思就是:通过获取yd_wap_news的宽度,然后设置width等于它的0.485,差不多是一半的值,让高度值也等于这个值。然后并将这个值赋给了yl_auto这个类,也就是给了图片。

   <script>
         var all_pm = $(".yd_wap_news3").width();
          width = all_pm * 0.485;
          height = width / 1;
          w = width.toString();
          w = w + "px";
          h = height.toString();
          h = h + "px";
          $(".yl_auto").css("width", w);
          $(".yl_auto").css("height", h);
          </script>
      </section>


最后我们可以得到:

  
      <!--产品列表-->
      <section class="yd_wap_news3 w_wap_news2">
        <h2>
          <i>
          </i>
          <em>
            <a href="/qiudong/" title="产品展示">更多</a></em>产品展示</h2>
        <ul>
        
          <li>
            <a href="/jack/52.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" style="width: 254.14px; height: 254.14px;">
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/51.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" style="width: 254.14px; height: 254.14px;">
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/50.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" style="width: 254.14px; height: 254.14px;">
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/49.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" style="width: 254.14px; height: 254.14px;">
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/48.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" style="width: 254.14px; height: 254.14px;">
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/47.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" style="width: 254.14px; height: 254.14px;">
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/46.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" style="width: 254.14px; height: 254.14px;">
              <span>测试产品</span></a>
          </li>
        
          <li>
            <a href="/jack/45.html" target="_self">
              <img src="/style/img/5ccfe345549f7.jpg" alt="" class="yl_auto" style="width: 254.14px; height: 254.14px;">
              <span>测试产品</span></a>
          </li>
        
         
        </ul>
        
      </section>
          <!--产品列表-->

看到变化了吗?强加了STYLE将给了宽度和高度。


本文》评论

留言