仅在移动设备上显示滑块而不是桌面

时间:2017-04-28 11:43:52

标签: javascript jquery html css media-queries

如何通过根据设备宽度替换现有HTML代码来动态修改DOM。使用@media查询隐藏或显示内容,尝试使用 .after 方法修改Jquery调整大小函数。需要帮助

DEMO JSFIDDLE

使用过的代码

function resizefunction(){
 if ($(window).width() < 500)
 {
   var $leftsec = $(".active li:nth-child(4)").html();
   var $rightsec = $(".active li:nth-child(5)").html();
   $( ".active" ).after( "<li>" + $leftsec+ "</li><li>" + $rightsec+ "</li>" );
   $(".active div:nth-child(4)").css("display","none");
   $(".active div:nth-child(5)").css("display","none");
 }
 else
 {
   console.log('Resize > 500');
 }
};

桌面版

<html>
 <body>
 <ul class="parent">
  <li class="active">1st slider
  <ul class="child">
   <li>1</li>
   <li>2</li>
   <li>3</li>
   <li>4</li>
 </ul>
 </li>
  <li>2st slider
   <ul class="child">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
  </ul>
  </li>
 </ul>
 </body>
</html> 

Desktop Version showing 4 boxes

移动版

<html>
 <body>
  <ul class="parent">
   <li class="active">1st slider
    <ul class="child">
     <li>1</li>
     <li>2</li>
    </ul>
   </li>
   <li>2st slider
     <ul class="child">
      <li>3</li>
      <li>4</li>
     </ul>
   </li>
  </ul>
 </body>
</html>

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

您是否偶然想知道函数$(window).resize()?参考: - https://api.jquery.com/resize/

$(window).resize(function() {
  // This will execute whenever the window is resized
  $(window).width(); // New width
});

上述代码的参考: - http://www.sweet-web-design.com/wordpress/detecting-browser-window-size-resize-with-jquery/1614/