dongsou3041 2019-06-14 11:21
浏览 63
已采纳

使用JavaScript删除屏幕大小低于某个像素的div

I'm a newbie in coding. I knew this can be done by css, but want to do it by using JavaScript. I have a div tag and would like to not show it under 630px screen size. I searched this site and find this JavaScript code in another question and I liked it:

if( window.innerWidth > 630 ) {
//Your Code
}

But as I'm newbie I'm not familiar on how to insert it in my PHP code and where to insert div so it only works for screen above 630px.

  • 写回答

2条回答 默认 最新

  • duanjia7912 2019-06-14 11:50
    关注

    Here is a way to hide a div when the width of the screen is smaller then 700px

    function myFunction(x) {
      if (x.matches) { // If media query matches
        document.getElementById("divIWantedToHide").style.visibility = "hidden";
      } else {
        document.getElementById("divIWantedToHide").style.visibility = "visible";
      }
    }
    
    var x = window.matchMedia("(max-width: 700px)")
    myFunction(x) // Call listener function at run time
    x.addListener(myFunction)
    <div id="divIWantedToHide">
    tset
    </div>

    Fiddle

    Personally I would recommend you to use CSS for this to be more precise media querys.

    @media only screen and (max-width: 700px) {
      #divIWantedToHide {
        display: none;
      }
    }
    <div id="divIWantedToHide">
    tset
    </div>

    Fiddle

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器