分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

利用JS去做响应式布局

发布时间:2023-09-06 01:49责任编辑:林大明关键词:暂无标签
利用JS去做响应式布局
js动态改变布局方式
// 取浏览器可视区高宽 var lw = $(window).width(); var lh = $(window).height();
// 页面加载完毕后执行 $(function () {     // 加载完毕后设置body的高度和宽度     $(document.body).css({ "width": lw, "height": lh });     // 新的高度 = lh - (Navigation + Footer + Banner)     // 如果没有 Banner 可以不加     $(".Content").css("height", lh - 90); });
// 当窗口高宽改变时触发 $(window).resize(function () {     // 取窗口改变后的高度和宽度     var rw = $(window).width();     var rh = $(window).height();
    // 重置body的高度和宽度     $(document.body).css({ "width": rw, "height": rh });
    // 新的高度 = rh - (Navigation + Footer + Banner)     // 如果没有 Banner 可以不加     $(".Content").css("height", rh - 90); });
 
响应式web设计之@media
两种方式,一种是直接在link中判断设备的尺寸,然后引用不同的css文件:
<link rel="stylesheet" type="text/css" href="styleA.css" media="screen and (min-width: 400px)">
意思是当屏幕的宽度大于等于400px的时候,应用styleA.css
在media属性里:
screen 是媒体类型里的一种,CSS2.1定义了10种媒体类型and 被称为关键字,其他关键字还包括 not(排除某种设备),only(限定某种设备)(min-width: 400px) 就是媒体特性,其被放置在一对圆括号中。
<link rel="stylesheet" type="text/css" href="styleB.css"  media="screen and (min-width: 600px) and (max-width: 800px)">
意思是当屏幕的宽度大于600小于800时,应用styleB.css
另一种方式,即是直接写在<style>标签里:
@media screen and (max-width: 600px) { /*当屏幕尺寸小于600px时,应用下面的CSS样式*/
  .class {
    background: #ccc;
  }
}
写法是前面加@media,其它跟link里的media属性相同。
Max Width
下面的样式会在可视区域的宽度小于 600px 的时候被应用。
@media screen and (max-width: 600px) {
  .class {
    background: #fff;
   你的样式
  }
}
Min Width
下面的样式会在可视区域的宽度大于 900px 的时候被应用。
@media screen and (min-width: 900px) {
  .class {
    background: #fff;
  }
}
Multiple Media Queries
你还可以使用过个匹配条件,下面的样式会在可视区域的宽度在 600px 和 900px 之间的时候被应用。
@media screen and (min-width: 600px) and (max-width: 900px) {
  .class {
    background: #fff;
  }
}

利用JS去做响应式布局

原文地址:https://www.cnblogs.com/oycyqr/p/8806907.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved