美图秀秀拼接渐变过渡

by Anthony Ng

由Anthony Ng

如何使用Web组件创建渐变过渡 (How to use web components to create gradient transitions)

In this article, we will learn about Web Components. We will build a simple Web Component that transitions its gradient color.

在本文中,我们将学习Web组件。 我们将构建一个简单的Web组件,以转换其渐变颜色。

Web Components are a suite of different technologies that allow you to create reusable custom elements. Using a custom element is no different than using a <div />. You can create instances in your HTML. You can create an instance with JavaScript. You can attach event listeners to custom elements.

Web组件是一套不同的技术,可让您创建可重复使用的自定义元素。 使用自定义元素与使用<div />没什么不同。 您可以在HTML中创建实例。 您可以使用JavaScript创建实例。 您可以将事件侦听器附加到自定义元素。

Have you ever looked at the HTML specs and thought the authors left out an important element? This is the solution for you. Custom elements provide a way for developers to build their own fully-featured DOM elements.

您是否曾经看过HTML规范,并认为作者遗漏了重要元素? 这是您的解决方案。 自定义元素为开发人员提供了一种构建自己的功能齐全的DOM元素的方法。

自定义元素和Web组件之间的区别? (Difference between Custom Elements and Web Components?)

Many use the terms Custom Elements and Web Components interchangeably. Web Components are a suite of different technologies, which includes Custom Elements, Shadow DOM, and HTML Imports. Custom Elements has its own specifications (see here).

许多术语可互换使用“自定义元素”和“ Web组件”。 Web组件是一套不同的技术,其中包括“自定义元素”,“阴影DOM”和“ HTML导入”。 Custom Elements有其自己的规范( 请参阅此处 )。

Web Components are a native feature of the browser. You don’t need external libraries to use this functionality. You can see the browser support table here labeled Browser Support.

Web组件是浏览器的本机功能。 您不需要外部库即可使用此功能。 您可以在此处看到标记为Browser Support的浏览器支持表 。

You can see the support for Custom Elements here.You can see the support for templates here.You can see the support for Shadow DOM here.

您可以在此处查看对“自定义元素”的支持。 您可以在此处查看对模板的支持。 您可以在此处查看对Shadow DOM的支持。

这是React吗? (So this is React?)

React and Web Components solve different problems. Web Components provide strong encapsulation for reusable components. React provides a declarative library that keeps the DOM in sync with your data.React makes no differentiation between a native HTML element and a Web Component. It would handle your custom built Web Component like it does a normal HTML element.

React和Web组件解决了不同的问题。 Web组件为可重用的组件提供了强大的封装。 React提供了一个声明式库,使DOM与您的数据保持同步.React不会在原生HTML元素和Web组件之间进行区分。 它会像处理普通HTML元素一样处理您自定义构建的Web组件。

See this example of a React application using a Web Component.

请参阅此使用Web组件的React应用程序的示例。

The React documentation also shows how you can use React within your Web Components. I haven’t found a scenario that would warrant importing React.

React文档还显示了如何在Web组件中使用React。 我还没有找到可以导入React的场景。

让我们过渡渐变背景 (Let’s transition gradient backgrounds)

We’re going to build a gradient Web Component like the one below.

我们将构建一个类似于下面的渐变Web组件。

Notice how it transitions between gradient backgrounds. We cannot transition backgrounds by default. See Codepen here.

注意它是如何在渐变背景之间转换的。 默认情况下,我们无法转换背景。 请参阅此处的Codepen 。

But we can transition opacity. See Codepen here.

但是我们可以转变不透明度。 请参阅此处的Codepen 。

We can take advantage of this with CSS pseudo-classes to get the desired effect. See Codepen here.

我们可以将其与CSS伪类一起利用以获得所需的效果。 请参阅此处的Codepen 。

We can take advantage of this with the CSS “before” pseudo-class to get the desired effect. See Codepen here.

我们可以利用CSS“ before”伪类来利用这一点,以获得所需的效果。 请参阅此处的Codepen 。

There is a layer (<div />) with a gradient color. There is a second layer (div::before) with a different gradient color. This second layer sits on top of the first layer and has opacity: 0. To start the gradient transition, we transition the second layer’s opacity from 0 to 1. This gives us the effect that the gradient is transitioning.

有一个具有渐变颜色的图层( <div />)。 还有另一个具有不同渐变颜色的ayer (div:: before)。 该第二层位于所述第一层的顶部d has opac两者均:0.要启动梯度过渡,我们转换所述第二层的不透明度,从0到1。这使我们认为梯度被转变的效果。

As a developer, that’s a lot of things you have to know about. Wouldn’t it be nice to have a simple, declarative way of using this gradient? Imagine an HTML element called <my-gradient-background />. It accepts a gradient attribute that takes a gradient color, like “red, white, blue”. When we change the gradient, the gradient color will transition like we want. That is what we are going to create.

作为开发人员,您需要了解很多事情。 有一个简单的,声明性的方式使用此渐变会不好吗? 想象一个名为<my-gradient-background />HTML元素。 它ACCE pts a gr adient属性,需要一个渐变的颜色,像“红,白,蓝”。 当我们畅e the gr adient,渐变颜色过渡会像我们想要的。 那就是我们要创造的。

构建Web组件 (Building the web component)

To create a new Web Component, we declare a new class that extends HTMLElement.

为了创建一个新的Web组件,我们声明一个扩展HTMLElement的新类。

If you want to extend the functionality of an existing HTML element, you can extend from them instead. For example, to extend the functionality of a <p />, you would extend HTMLParagraphElement.

如果要扩展现有HTML元素的功能,则可以从其扩展。 例如,要扩展<p />的功能,您可以xtend HTMLParagraphE元素。

We attach a shadow root to our web component. The shadow DOM API lets us attach DOM to our gradient element. This shadow DOM is encapsulated in our component and is (generally) hidden from the rest of the DOM. You can read more about Shadow DOM here.

我们将影子根附加到我们的Web组件。 影子DOM API使我们可以将DOM附加到渐变元素上。 此影子DOM封装在我们的组件中,(通常)对其余DOM隐藏。 您可以在此处阅读有关Shadow DOM的更多信息 。

In our shadow DOM, we add some styling for the gradient element. We use a <div class=”after” /> instead of a pseudo-element here. This is because we want to reference this layer with JavaScript. We can’t reference pseudo-elements with JavaScript.

在我们的影子DOM中,我们为渐变元素添加了一些样式。 我们在这里使用<div class=”after” />而不是伪元素。 这是因为我们要使用JavaScript引用此层。 我们无法使用JavaScript引用伪元素。

The host element is the gradient element itself. We can style it as if it were a <div /> element.

host元素是渐变元素本身。 我们可以将其样式设置为好像是一个<div />元素。

In the observed attributes, we return a list of HTML attributes that we want to watch. When these watched attributes change, our a callback function will fire.

在观察到的属性中,我们返回要观看HTML属性的列表。 当这些受监视的属性更改时,我们的回调函数将触发。

Our attributeChangedCallback function fires whenever an observed attribute changes. We get 3 arguments in our callback function. The first argument is the name of the attribute that changed. The second argument is the value of the attribute before it changed. The third argument is the value of the attribute after it changed.

只要观察到的属性发生更改,我们的attributeChangedCallback函数就会触发。 我们在回调函数中得到3个参数。 第一个参数是已更改属性的name 。 第二个参数是属性更改之前的值。 第三个参数是属性更改后的值。

In our callback function, we update our “after” element.

在回调函数中,我们更新“ after”元素。

We update our “after” element’s background color with the new gradient color. We also set its opacity to 1. Our “after” element will start fading in, creating our desired effect. We want to do some cleanup code when the “after” element has finished fading in.

我们使用新的渐变色更新“ after”元素的背景色。 我们还将其不透明度设置为1。“ after”元素将开始逐渐消失,从而产生所需的效果。 当“ after”元素逐渐消失时,我们想做一些清理代码。

Our “after” element does all the work of creating the gradient transition effect. We set our “host” element to the new gradient color. We hide the “after” element so that it’s ready for the next fade in. That is all the clean up we need.

我们的“之后”元素完成了创建渐变过渡效果的所有工作。 我们将“宿主”元素设置为新的渐变颜色。 我们隐藏“ after”元素,以便为下一次淡入做好准备。这就是我们需要的所有清理工作。

To use this new Web Component, we have to define it like so.

要使用这个新的Web组件,我们必须像这样定义它。

Now, you’ll be able to use <my-gradient-background /> like a normal HTML element.

现在,您将能够像普通HTML元素一样使用<my-gradient-background />。

You can view the complete code here. Feel free to download it with npm install — save my-gradient-background.

您可以在此处查看完整的代码。 随时使用npm install — save my-gradient-background下载它npm install — save my-gradient-background

参考文献 (References)

  • https://developers.google.com/web/fundamentals/web-components/customelements

    https://developers.google.com/web/fundamentals/web-components/customelements

  • https://www.webcomponents.org/introduction

    https://www.webcomponents.org/introduction

  • https://developer.mozilla.org/en-US/docs/Web/Web_Components

    https://developer.mozilla.org/zh-CN/docs/Web/Web_Components

  • https://reactjs.org/docs/web-components.html

    https://reactjs.org/docs/web-components.html

  • https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements

    https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements

  • https://w3c.github.io/webcomponents/spec/custom/#custom-elements

    https://w3c.github.io/webcomponents/spec/custom/#custom-elements

翻译自: https://www.freecodecamp.org/news/use-web-components-to-create-gradient-transitions-f9aad648824a/

美图秀秀拼接渐变过渡

美图秀秀拼接渐变过渡_如何使用Web组件创建渐变过渡相关推荐

  1. 美图秀秀拼接渐变过渡_使用Granim.js创建漂亮的渐变过渡

    网页设计充满美感和令人愉悦的界面设计. 一些功能可以使用,而其他功能仅用于展示. 渐变过渡 仅用于显示,但有很大的作用! 使用Granim.js ,您可以构建自定义的全色渐变过渡 ,使其看起来平滑且可 ...

  2. 给图片下方加水印_别再看不起美图秀秀啦,想要做长图,批量加水印,用它超级方便...

    Hello大家好,我是撒娇的小肉片. 时隔超长时间的更新,不知道还有多少人记得我哈哈.今天想要和大家分享的是如何批量加水印,如何拼成长图,操作简单,你值得拥有哦~ 最重要的是免费!免费!完全免费! 由 ...

  3. [原创]ASP.NET MVC调用美图秀秀开放平台拼图实现

    项目中涉及到图片的美化和拼接的功能,于是用了美图秀秀开放平台的api 美图秀秀开放平台地址:http://open.web.meitu.com/ 具体步骤如下: 1.创建MeiTuUpload.asp ...

  4. 美图秀秀快速将多张图片拼接成长图的方法

    描述:美图秀秀快速将多张图片拼接成长图的方法 步骤: 打开美图秀秀,选择拼图 打开图片,选择图片拼接 选择添加图片 图片就长图拼接了

  5. mfc tabcontrol 修改白色背景_初级会计报名准备工作如何使用美图秀秀PC版修改照片尺寸、格式、大小...

    点击上方蓝字关注我们 证件照是我们生活中常用的东西,大学考证需要证件照,制作简历也需要用到证件照,工作有时也需要用到,然而最让我们头疼的是每次报名证件照的要求都不一样,其中底色和尺寸是经常需要修改的, ...

  6. 苹果手机怎么在照片上添加文字_手机美图秀秀怎么给图片添加文字

    美图秀秀已经成为我们手机中必不可少的一款软件,这款软件可以使我们每个人都能成为美图大师,今天我们就来迈向大师的第一步,给照片添加文字水印. 工具/原料 美图秀秀软件 手机一部 方法/步骤 1 首先我们 ...

  7. 安卓机器人做图软件_绘画机器人andy下载-美图秀秀绘画机器人下载v7.0.0.0 安卓版-西西软件下载...

    美图秀秀美图绘画机器人是美图秀秀最近上线的一个智能绘画功能,这个新的AI机器人andy可以将你的照片秒变插画.相信不少人都见识过andy的功能了,这个新功能上线的第一时间就有不少明星试用过了,软件转换 ...

  8. 安卓机器人做图软件_美图秀秀绘画机器人app下载-美图绘画机器人Andy最新版下载v7.0.0.0-西西软件下载...

    美图绘画机器人Andy最新版是美图秀秀最新推出的AI智能绘画机器人,名叫Andy,这款软件可以让你的照片秒变插画,非常不错的一款软件,非常好玩,效果也非常的棒,欢迎大家前来西西下载美图绘画机器人And ...

  9. 安卓机器人做图软件_绘图机器人andy软件下载-美图秀秀绘图机器人andy_5577安卓网...

    绘图机器人andy软件已经上线,为期待的用户朋友们第一时间带来下载.功能非常的强大哦.大家上传一张自拍照,机器人Andy就可以为你们画出不同风格的插画像.感兴趣的朋友赶紧来下载试试吧 [绘图机器人an ...

  10. 安卓机器人做图软件_绘画机器人andy app下载-美图秀秀绘画机器人v7.0安卓版_5577安卓网...

    本站为大家带来绘画机器人andy app下载资源.美图秀秀绘画机器人在哪里.美图秀秀绘画机器人怎么用?这是美图新上线的图片处理功能,一秒拥有属于自己的专属肖像画,画风穿越二次元! [美图秀秀绘画机器人 ...

最新文章

  1. linux常用文本编辑器nano/vi/vim
  2. 自然语言系列学习之表示学习与知识获取(七)利用关系路径进行关系抽取
  3. 从1.5k到18k, 一个程序员的5年成长之路(分享)
  4. 织梦 css里的图片标签,织梦{dede:field.body /}中用CSS的expression参数控制图片大小
  5. Docker swarm集群搭建教程
  6. 多个异步之间的协作方案
  7. 毕啸南专栏 | 对话今日头条副总裁马维英:有技术也要有价值观
  8. THINKPHP_关联模型MANY_TO_MANY
  9. 前后端分离的思考与实践(六)
  10. Java快逸报表展现demo,快逸报表展示图片—来自数据库中的图片
  11. 常见的协议号和端口号
  12. 重装系统显示“安装程序正在应用系统设置“静止不动
  13. phpnow安装教程
  14. Linux命令之md5sum的作用以及使用方法(md5是什么?)
  15. 自学MBA,我推荐你看这本《MBA必读12篇》
  16. ctrl+enter键
  17. 高校师生疫情管理系统
  18. 软件测试之linux环境搭建与操作Xshell、Xftp
  19. Python基础:异常处理
  20. 1.1.2 Java的应用领域

热门文章

  1. Jrebel激活服务,Jrebel激活,Jrebel激活码,Jrebel破解
  2. ftp服务器文件夹设置,ftp服务器可设置的文件夹
  3. godot常用的一些概念、组件(整理于官方教程)
  4. 专家不确定伊朗是否是银行 DDoS 攻击的幕后黑手
  5. kotlin入门最容易教程一(最全,最详细)
  6. 计算机主机ip地址题,计算机网络IP地址练习习题
  7. workbench动力学周炬_ANSYSWorkbench有限元分析实例详解(动力学)
  8. 485Modbus协议
  9. 蛋白质配体复合物-分子动力学模拟Gromacs
  10. GCJ-02火星坐标系和WGS-84坐标系转换关系