• vue 响应式布局组件A few years ago I started to hear a lot about Web Components. I got pretty excited and then totally forgot about them as the noise settled down. It turns out there has been some good...

    vue 响应式布局组件

    A few years ago I started to hear a lot about Web Components. I got pretty excited and then totally forgot about them as the noise settled down. It turns out there has been some good movement and support is starting to increase. Responsive Web Components, in particular, are set to really simplify how we approach responsive design. As promising as this sounds, there are four questions many developers out there will want to know before they start using them today:

    几年前,我开始听到很多有关Web组件的信息。 我非常兴奋,但随着噪音逐渐消失,他们完全忘记了它们。 事实证明,目前已有一些良好的运动,并且支持开始增加。 尤其是响应式Web组件旨在真正简化我们进行响应式设计的方式。 听起来如此有前途,但是许多开发人员在开始使用它们之前会想知道四个问题:

    Will they…


    • Fit into the current state of the web?

    • Work cross browser?

    • Adapt to fit the space they are dropped into?

    • Promote modular code?


    In this article, we will explore the answers to each of those questions!


    什么是Web组件? (What Are Web Components?)

    Essentially, they are a W3 specification that enables you to build your own HTML elements. If you literally don’t know anything about them it may be worth having a quick read up about them as some of the terms I use in the article require a basic understanding.

    本质上,它们是W3规范,使您能够构建自己HTML元素。 如果您对它们一无所知,那么值得快速阅读一下,因为我在本文中使用的某些术语需要基本的理解。

    我为什么要使用它们? (Why Would I Want To Use Them?)

    A good example of when you might want to use Web Components would be when you are building UI that doesn’t exist natively in the browser — elements such as color pickers, carousels, accordions or typeaheads. Alternatively, you are likely to have some components that you use all the time across various sites that you are sick of rebuilding every time. These are things like social buttons, subscribe forms or notifications. With Web Components you can bundle up your markup, styles and scripts into encapsulated modules using HTML tags like <color-picker></color-picker> to reference them. This will seem familiar if you have used Angular directives or React components but the benefit here is that it is native to the browser and framework agnostic.

    当您要构建浏览器本身不存在的UI时(例如颜色选择器,轮播,手风琴或预先键入),可以使用Web组件作为一个很好的例子。 另外,您很可能会在各个站点上经常使用某些组件,而您每次都想重建它们。 这些是社交按钮,订阅表单或通知之类的东西。 借助Web组件,您可以使用HTML标签(例如<color-picker></color-picker>将标记,样式和脚本捆绑到封装的模块中,以引用它们。 如果您使用过Angular指令或React组件,这似乎很熟悉,但是这样做的好处是,它对于浏览器和框架无关。

    Web Components enable you to build pieces of UI once and use them everywhere. This is ideal for projects like a living styleguide where you want to build a library with a single source of truth for each of your components. This approach means repeating less code and having portable components that you or your team can simply drop references to into your interface.

    Web组件使您可以一次构建UI并将其在任何地方使用。 对于像生活风格指南这样的项目,它是一个理想的项目,在该项目中,您想为每个组件的单个事实来源建立一个库。 这种方法意味着减少重复的代码,并拥有可移植的组件,您或您的团队可以简单地将引用插入到您的界面中。

    Another big advantage of using Web Components is the ability to package these components up and share them with other developers or website owners. Essentially, the consumer can put an import statement in their web page such as:

    使用Web组件的另一个主要优点是能够打包这些组件并将其与其他开发人员或网站所有者共享。 从本质上讲,消费者可以在其网页上放置一个导入语句,例如:

    <link rel="import"

    Then use the custom element tag you’ve defined in that component. With this imported into their webpage, they can have as many instances of the custom component on their page as they wish. In the subscribe form example, we could use the tag <subscribe-form></subscribe-form> in multiple locations on the page. Of course, if you are doing this, you need to ensure that the beautiful component you have crafted is extremely flexible and will work across a range of devices and screen sizes.

    然后使用您在该组件中定义的自定义元素标签。 将其导入到他们的网页后,他们可以在其页面上根据需要拥有任意数量的自定义组件实例。 在订阅表单示例中,我们可以在页面上的多个位置使用标签<subscribe-form></subscribe-form> 。 当然,如果要这样做,则需要确保精心制作的精美组件非常灵活,并且可以在各种设备和屏幕尺寸上使用。

    将这些坏男孩变成响应式Web组件 (Turning These Bad Boys Into Responsive Web Components)

    I feel the best way to demonstrate is by example, so I’ll continue with the subscribe form which is a common piece of UI that doesn’t change much between different sites and implementations.


    Here’s one I made earlier:


    Screenshot of email subscription form, two input fields one for name and one for email, side by side

    Inside my Web Component template I have some basic markup for the form.


    <ul class="form"> 
      <li class="form__item">
        <label for="name">Name:</label>
        <input id="name" name="name" type="text">
      <li class="form__item">
        <label for="name">Email:</label>
        <input id="name" name="name" type="text">

    My markup and CSS are tucked away inside my component making use of the Shadow DOM part of the specification. This gives me encapsulation, allowing me to use label and input selectors freely in my code without worry of other styles bleeding in or mine bleeding out into the parent site.

    利用规范的Shadow DOM部分,我的标记和CSS被隐藏在组件内部。 这给了我封装性,使我可以在代码中自由使用labelinput选择器,而不必担心其他样式渗入或渗入父站点。

    For clarity and brevity, I’m not going to write out the styles other than ones used for layout. I’ve used a combination of float: left and display: table to achieve this layout to prevent breaking in any widths.

    为了清楚和简洁起见,我将不写除用于布局的样式以外的其他样式。 我使用了float: leftdisplay: table来实现此布局,以防止以任何宽度折断。

    .form__item {
      display: table;
      float: left;
      width: 50%;  
    label {
      display: table-cell;
      width: auto;
    input {
      display: table-cell;
      width: 100%;

    I’ll be using HTML imports to pull it into the SitePoint site as a demonstration:


    Screenshot of email subscription form imported into main body of SitePoint site

    Now we’re all set up, let’s look at some responsive techniques.


    媒体查询 (Media Queries)

    The classic approach that we know and love is still alive and well inside Responsive Web Components. These can be applied inside your template when you code your component if you want to bake in some breakpoints, or you can apply a hook (for example, by using a class) to the container if you want to leave that decision to your consumer. In this case, all we need to do is strip the floats and adjust them to be full width.

    在响应式Web组件中,我们知道和喜欢的经典方法仍然有效。 如果您要在某些断点处进行烘焙,则可以在对组件进行编码时将其应用于模板中,或者,如果您要将该决定留给使用者,则可以对容器应用一个钩子(例如,通过使用类)。 在这种情况下,我们要做的就是去除浮子并将其调整为全宽。

    @media (max-width: 30em) {
      .form__item {
        float: none;
        width: 100%;
    Screenshot of email subscription form below 30em, form fields are now stacked instead of inline

    However, media queries aren’t going to be enough to save us this time.


    I’m happy with my sign up form styles and it works fine in the body, but what happens when the website author wants to chuck it into their sidebar? Suddenly my form looks squashed and isn’t really usable.

    我对我的注册表单样式感到满意,并且可以很好地工作,但是当网站作者希望将其插入到侧边栏时会怎样? 突然,我的表单看起来很紧缩,并没有真正可用。

    Screenshot of email subscription form, squashed up in the Sitepoint website sidebar

    This is because the component is not aware of its boundaries – it has no context.


    The whole point of Web Components is you can drop them anywhere and they’ll just work right? So obviously this won’t do, but there are a few techniques you can use to ensure your components are responsive and contextual.

    Web组件的全部要点是您可以将它们放到任何地方,它们会正常工作吗? 因此显然这是行不通的,但是您可以使用一些技术来确保您的组件具有响应能力和上下文相关性。

    Flexbox欺骗 (Flexbox Trickery)

    If you’re looking at using Web Components, odds are you’re only supporting modern browsers and not worrying about IE9, in which case, a bit of Flexbox magic might be perfect for you to turn these into Responsive Web Components. This is my preferred approach as it doesn’t require any additional JavaScript.

    如果您正在使用Web组件,很可能只支持现代浏览器,而不必担心IE9,在这种情况下,一些Flexbox魔术可能非常适合您将它们变成响应式Web组件。 这是我的首选方法,因为它不需要任何其他JavaScript。

    Once again, the code sample here is purely showing the layout styles I’ve used:


    .form {
      display: flex;
      flex-wrap: wrap;
    .form__item {
      align-items: center;   
      display: flex;
      flex: 1 0 320px;
      flex-wrap: wrap;
      max-width: 100%;
    label {
      flex: 1 0 90px;
    input {
      flex: 1 0 230px;
      width: 100%;

    By setting the container to display: flex; flex-wrap: wrap, the .form__item elements will appear side-by-side, but we still need to make a few adjustments so that when the container gets too small they will stack nicely.

    通过将容器设置为display: flex; flex-wrap: wrap display: flex; flex-wrap: wrap.form__item元素将并排显示,但是我们仍然需要进行一些调整,以便当容器变得太小时,它们可以很好地堆叠。

    Inside the .form__item, I’ve used the flex shorthand of flex: 1 0 320px; which translates in flex terms to — grow of one, flex shrink of zero, and flex basis of 320 pixels. Having flex-wrap set to wrap means it won’t be any smaller than the basis we set (320px) but setting it to flex means it will take up the rest of the available space.

    .form__item内部,我使用了flex的flex缩写flex: 1 0 320px; 以flex的术语表示为-增长1,flex缩小为0,flex基础为320像素。 将flex-wrap设置为wrap意味着它不会小于我们设置的基础(320像素),但是将其设置为flex意味着它将占用剩余的可用空间。

    I have given a similar treatment to the label and input elements. Their combined flex values total 320, which means they will behave as desired at smaller container sizes.

    我对labelinput元素进行了类似的处理。 它们的组合弯曲值总计为320,这意味着它们在较小的容器尺寸下将表现出所需的性能。

    With these adjustments in place, let’s take a look at that same form in SitePoint’s sidebar:


    Screenshot of email subscription form, sitting nicely in the sidebar with form elements stacked using Responsive Web Components

    Much better! However, there is more than one way to skin a cat.

    好多了! 但是,有多种方法可以给猫皮化。

    元素查询 (Element Queries)

    The idea of element queries is that you are able to emulate media query type functionality but at a component level — very handy for Responsive Web Components. Essentially, imagine being able to write something like this in your CSS:

    元素查询的想法是,您可以在组件级别上模拟媒体查询类型的功能-对于响应式Web组件非常方便。 本质上,想象一下能够在CSS中编写如下代码:

    .form__item[max-width~="30em"] {
      /* Styles here */

    With element queries that is exactly what we can do. Pretty powerful right?

    使用元素查询正是我们可以做的。 很强大吧?

    Unfortunately, this technique isn’t available natively due to browser vendors being concerned with the potential to cause infinite loops. However, some smart people have written plugins to enable this functionality.

    不幸的是,由于浏览器供应商担心引起无限循环的可能性,因此无法自然使用该技术。 但是,一些聪明人编写了插件来启用此功能。

    In my example, I have used CSS Element Queries by Marc J Schmidt which is a really nice implementation. There are also the following similar projects:

    在我的示例中,我使用了Marc J Schmidt的 CSS元素查询 ,这是一个非常不错的实现。 也有以下类似的项目:

    Essentially what they do is use JavaScript to detect the width of the component and modify an attribute so that you are able to style it in CSS. Some are event driven and some based on resize.

    本质上,他们所做的是使用JavaScript检测组件的宽度并修改属性,以便您可以在CSS中对其进行样式设置。 有些是事件驱动的,有些则基于调整大小。

    .form__item[max-width~="30em"] {
      float: none;
      width: 100%;

    With the combination of one of these plugins and a small code snippet similar to the above we are able to get the exact same result as the flexbox implementation in the sidebar.


    属性 (Attributes)

    One of the cool things we can offer to consumers of our Responsive Web Components is an API via attributes. For example, you could set up a attribute called layout and have sizes of “small” and “large”. Our consumer of the component could then use it as follows:

    我们可以为响应式Web组件的消费者提供的很酷的功能之一就是通过属性的API。 例如,您可以设置一个名为layout的属性,其大小分别为“小”和“大”。 然后,我们的组件使用者可以按以下方式使用它:

    <subscribe-form layout="small"></subscribe-form>

    This would trigger the styles for the small implementation.


    In the Shadow DOM, the containing element is known as the :host. An example could look like so:

    在Shadow DOM中,包含元素称为:host 。 一个示例如下所示:

    :host { 
      display: block; 
    :host([layout = "small"]) .form__item { 
      float: none;
      width: 100%;
    :host([layout = "large"]) .form__item { 
      display: table;
      float: left;
      width: 50%;   

    This would then allow the component consumer to write some simple JavaScript to flip between the different sizes that you’ve provided them. This means instead of loading additional JavaScript inside of your module, you can leave it up to the consumer to decide how they want to hook into it. This allows your component to become more future proof.

    然后,这将允许组件使用者编写一些简单JavaScript,以在您提供的不同大小之间切换。 这意味着您无需将其他JavaScript加载到模块中,而应由使用者自行决定如何挂钩。 这使您的组件变得更适合将来使用。

    自己尝试! (Try It Yourself!)

    If you’re interested in seeing the flexbox version I refer to in this demo in action on your own machine, you can do the following in Chrome:


    1. Install the CORS Chrome extension and turn it on. This is to allow HTML Imports to work from external sites.

      安装CORS Chrome扩展程序并将其打开。 这是为了允许HTML导入从外部站点进行。

    2. Either refresh this page or open any website in a new tab or window so the extension kicks in.

    3. In Chrome developer tools, add this inside the <head> tag:


      <link rel="import" href="http://wsh.webfactional.com/web_components/components/
    4. Finally, add:



      somewhere inside the <body> and you should see my subscribe form auto-magically pulled through. Enjoy!

      <body>内的某个位置,您应该会看到我的订阅表单自动神奇地通过。 请享用!

    结论 (Conclusion)

    The approaches above can actually be used for any UI development but they lend themselves particularly well to Responsive Web Components due to their reusable and shareable nature.


    If you haven’t checked out Web Components yet I would strongly encourage you to do so. While the full spec is currently only available natively in Chrome and Opera, the other browser vendors aren’t far behind.

    如果您尚未签出Web组件,我强烈建议您这样做。 虽然完整规范目前仅在Chrome和Opera中本地提供,但其他浏览器供应商也不甘落后

    If you’re interesting in learning more about Responsive Web Design, check out our new book, Jump Start Responsive Web Design, written by Chris Ward.

    如果您有兴趣学习有关响应式Web设计的更多信息,请查看克里斯·沃德(Chris Ward)撰写的新书《快速入门响应式Web设计》

    翻译自: https://www.sitepoint.com/responsive-web-components/

    vue 响应式布局组件

  • vue响应式布局ui库 自动响应 (autoresponsive-vue) Auto responsive grid layout library for Vue. Vue的自动响应网格布局库。 View demo 查看演示 Download Source 下载源 安装 (Installation) $ npm i ...


    自动响应 (autoresponsive-vue)

    Auto responsive grid layout library for Vue.



    安装 (Installation)

    $ npm i autoresponsive-vue --save-dev

    翻译自: https://vuejsexamples.com/autoresponsive-vue/


  • vue响应式布局与自适应方案

    千次阅读 2020-06-10 15:07:43
    layout布局 https://blog.csdn.net/jack_bob/article/details/79813114 vue自适应方案 https://blog.csdn.net/linkea_i/article/details/81092243
  • Vue的自动响应式网格布局
  • 平时开发中,Vue响应式系统让我们不再去操作DOM,只需关心数据逻辑的处理,极大地降低了代码的复杂度。而响应式系统也是Vue的核心,作为开发者有必要了解其实现原理! 简易版 以watch为切入点 watch是平时开发中...
  • “自己写的前端页面只能适配自己电脑,一旦再他人电脑上打开布局全乱” 就这个问题已经困扰我许久,尤其...以及css3的响应式布局 @media 首先来说elementui布局的响应式属性 elementUi将一行划分成24块,可以通过使用e



    以及css3的响应式布局 @media



    <el-col :span="4" v-for="(o, index) in 10" :key="index" :offset="1" :xs="{span:10,offset:1}" >

    上方的代码就实现了当我的浏览器的宽度 >768px 时,将一个el-col标签所占有的格数从4变成10,这样一行的数量就从原来的4改成了2



        @media screen  and (max-width: 700px){
            div {
                color: #42b983;


  • 1,设计稿的宽度为1920,在网上找了计算方式,总觉的不太对,大神请帮忙看下有没有问题。2,项目是pc端的,要适配移动端。我这个写出来后,移动端就变的特别小。而且pc端在缩放的时候都会挤到一起。...
  • Vue框架设置响应式布局

    万次阅读 多人点赞 2018-09-03 09:51:20
  • ヤッハロー、Kaiqisanすうう、一つふつうの学生プログラマである,学过响应式布局的一定知道页面的响应式设计都是通过css来实现的,但是在vue中,是否还可以通过编写css代码来实现呢? 回答: 可以,而且非常简单和...
  • vue项目中页面响应式布局设计方案

    千次阅读 2019-11-27 17:09:49
    针对全屏页面开发,兼容 19201080,以及 1366768 js设置原型属性$screen: 获取当前是什么屏幕 ...在主文件app.vue中设置: let _w = window.innerWidth, _h = window.innerHeight, isS = fals...
  • Vue实战篇十:响应式布局

    千次阅读 2020-11-06 14:36:35
    Vue基础篇一:编写第一个Vue程序 Vue基础篇二:Vue组件的核心概念 Vue基础篇三:Vue的计算属性与侦听器 Vue基础篇四:Vue的生命周期(秒杀案例实战) Vue基础篇五:Vue的指令 Vue基础篇六:Vue使用JSX进行动态渲染 ...
  • Vue使用ivew组件实现响应式布局

    千次阅读 2020-04-26 00:40:50
    1.vue-cli3.0中使用iview 链接: Vue-cli3.0中使用iview. 1.在控制台启动vue ui 2.在vue ui中搜索并安装iview 或者直接在控制台输入 cnpm install iview 3.安装好后重启项目 4.在main.js中引入 import iview from '...
  • 问题描述:选择哪个模块就显示什么 思路:通过标识来判断是否选择,动态控制列表长度及样式 页面渲染代码: ...div class="header_tabs">...li v-for="(item,index) in headertabs" :key="index" :class=...
  • Vue封装组件实现响应式布局

    千次阅读 2020-06-11 14:17:22
    这里写自定义目录标题Vue封装组件实现响应式布局1. 封装 Layout.vue组件2. Layout.vue组件使用 Vue封装组件实现响应式布局 主要思路:使用flex布局,左边主体内容区域根据页面宽度自适应,右边侧边栏固定宽度。使用...
  • Vue最显著的特性之一便是不太引人注意的响应式系统(reactivity system)。...本文将详细介绍Vue响应式系统的底层细节。追踪变化把一个普通JS对象传给Vue实例的data选项,Vue将遍历此对象所有的属性,并使用Object.def...
  • 简单讲一下响应式布局!!!响应式布局分为两种(1)对于pc端屏幕尺寸的自适应屏幕尺寸多为px像素为高度宽度的定义标准,故在编码中也多以px为width,height的属性值单位。这就产生了对于不同屏幕的不同高度宽度而产生...
  • Vue:PC端全局响应式布局

    千次阅读 2019-06-24 17:33:44
    PC端全局响应式布局 根据分辨率720到1080或者根据窗口尺寸 两个方法room和transform:scale(x,y) 1、css3的room 对px有效,对rem无效 由于resize事件可以以较高的速率触发, 因此事件处理程序不应该执行计算开销很大...
  • 基于Vue.js和Bootstrap构建和响应式管理面板
  • vue 移动端与PC端的响应式布局整理

    千次阅读 2019-12-04 10:11:59
    响应式布局大概就是三种方法: 首先必须在页面头部必须有mate声明的viewport, **1,**百分比布局。就是margin,padding啥的都是百分比。 **2,**媒体查询(@media),移动端优先首先使用的是min-width,PC端优先使用...
  • 基于postcss-pxtorem的vue项目响应式布局 最近在开发一个门户网站,用到了媒体查询响应式布局和postcss-pxtorem,下面做个小总结,方便以后使用。 安装amfe-flexible和postcss-pxtorem npm install amfe-flexible ...
  • a slider implement by vuejs
  • vue-cli3 + bootstrap3实现响应式布局

    千次阅读 2019-03-02 10:34:16
    类可以让图片支持响应式布局。其实质是为图片设置了 max-width: 100%;、 height: auto; 和 display: block; 属性,从而让图片在其父元素中更好的缩放。 对于图片的大小限制一定要在图片的父级元素进行限制。 5、...
  • 分享一种vue-lic中使用rem,实现响应式布局的超简单的方法。   第一步:封装设置rem的方法,放在公用类的js文件中,或者直接放在main.js文件中。 /** * 设置rem * @param pwidth * @param prem */ export ...
  • Vue 内部使用了 Object.defineProperty() 来实现数据响应式,通过这个函数可以监听到 set 和 get 的事件。 var data = { name: 'yck' } observe(data) let name = data.name // -> get value data.name = 'yyy' /...
  • 我们都知道,vue 最独特的特性之一,是非侵入性的响应式系统,数据模型仅仅是普通的 JavaScript 对象,而当我们修改它们时,视图会进行更新,其实就是让组件重新渲染。那么 vue 是怎样追踪到数据的变化并重新渲染...
  • 移动端和pc端,响应式设计布局

    千次阅读 2020-12-22 10:16:18
    1.什么是响应式 Web 设计?响应式 Web 设计让你的网页能在所有设备上有好显示。响应式 Web 设计只使用 HTML 和 CSS。响应式 Web 设计不是一个程序或Javascript脚本。2.响应式的作用:设计最好的用户体验友好的用户...
  • 001-Bootstrap简介.avi 002-Bootstrap下载.avi 003-Bootstrap项目环境配置.avi 004-Bootstrap初体验.avi 005-Emmet语法-上集.avi 响应式布局的基础教程,有需要前端开发教程的朋友可以看看。
  • 实现响应式布局。根据网格的宽度来实现不同的布局。这是一个对应写好的文档。github上文档在根目录中:https://github.com/lison16/vue-cource/blob/master/doc/vue-cli3%E4%B8%AD%E4%BD%B...
  • vue-cli 中实现响应式布局

    千次阅读 热门讨论 2021-02-14 09:28:03
    vue-cli 中实现响应式布局 我们在进行前端开发中必然会遇到PC与移动端的适配,面对这样的问题有些公司会准备两个页面,移动是移动,PC是PC,而响应式布局就是根据用户不同的机型展示不同的页面,废话不多说,直接贴...



1 2 3 4 5 ... 20
收藏数 10,265
精华内容 4,106


vue 订阅
友情链接: md5.rar