search
HomeWeb Front-endHTML Tutorial超炫喷洒墨水动画过渡效果的打开模态窗口特效_html/css_WEB-ITnose

简要教程

这是一款使用CSS3和jQuery来制作的炫酷喷洒墨水式打开模态窗口特效。该特效在点击按钮时通过一张PNG墨水喷洒的雪碧图和steps()函数来制作墨水散开的效果,整个效果就像在屏幕上撒上了墨水,使屏幕改变了一个颜色。

通过一张制作好的雪碧图,以及CSS的steps()函数,我们可以制作出各种平滑过渡的动画效果。一些效果的图片可以直接从某些视频video中提取,使用的工具是After Effects。你可以将需要的视频的帧转换为连续的PNG图片,然后将这些图片拼接为雪碧图。

查看演示      下载插件

使用方法

HTML结构

该效果的HTML结构分为3个主要部分:main.cd-main-content是页面的主体部分,div.cd-modal是模态窗口,div.cd-transition-layer是一个动画过渡层。

<main class="cd-main-content">  <div class="center">    <h1 id="Ink-Transition-Effect">Ink Transition Effect</h1>    <a href="#0" class="cd-btn cd-modal-trigger">Start Effect</a>  </div></main> <!-- .cd-main-content -->  <div class="cd-modal">  <div class="modal-content">    <h1 id="My-Modal-Content">My Modal Content</h1>         <p>      Lorem ipsum dolor sit amet, consectetur adipisicing elit.       Ad modi repellendus, optio eveniet eligendi molestiae?       Fugiat, temporibus!     </p>  </div> <!-- .modal-content -->    <a href="#0" class="modal-close">Close</a></div> <!-- .cd-modal -->  <div class="cd-transition-layer">   <div class="bg-layer"></div></div> <!-- .cd-transition-layer -->

CSS样式

模态窗口.cd-modal开始时通过visibility: hidden来隐藏,高度和宽度都设置为100%,并且使用固定定位方式。

当用户点击了触发按钮a.cd-modal-trigger,模态窗口通过.visible属性设置为可见。

.cd-modal {  position: fixed;  top: 0;  left: 0;  z-index: 3;  height: 100%;  width: 100%;  opacity: 0;  visibility: hidden;}.cd-modal.visible {  opacity: 1;  visibility: visible;}

div.cd-transition-layer元素用于创建喷洒墨水效果的动画过渡层。它初始时被设置为:visibility: hidden,高度和宽度都设置为100%,并且使用固定定位方式。

.cd-transition-layer {  position: fixed;  top: 0;  left: 0;  z-index: 2;  height: 100%;  width: 100%;  opacity: 0;  visibility: hidden;  overflow: hidden;}

它的子元素div.bg-layer使用雪碧图作为背景图片,并设置background-size: 100%,100%高度和2500%的宽度(因为在这个DEMO中,ink.png有25帧)。开始时,ink.png的第一帧被居中放置在div.cd-transition-layer中。

.cd-transition-layer .bg-layer {  position: absolute;  left: 50%;  top: 50%;  transform: translateY(-50%) translateX(-2%);  height: 100%;  /* our sprite is composed of 25 frames */  width: 2500%;  background: url(../img/ink.png) no-repeat 0 0;  background-size: 100% 100%;}

上面的居中方式需要注意的是,通常我们使用绝对定位居中一个元素都是使用如下的代码:

position: absolute;left: 50%;top: 50%;transform: translateY(-50%) translateX(-50%);

在这个DEMO中,我们需要居中ink.png雪碧图的第一帧,由于div.bg-layer的宽度是它的父元素宽度的25倍,所以要使用translateX(-(50/25)%)来进行居中。

为了创建喷洒墨水的动画,我们需要修改div.bg-layer的值。可以定义如下的@keyframes帧动画。

@keyframes cd-sequence {  0% {    transform: translateY(-50%) translateX(-2%);  }  100% {    transform: translateY(-50%) translateX(-98%);  }}

通过上面的帧动画,在动画结束时,ink.png雪碧图的最后一帧将在div.cd-transition-layer中居中。

对于上面100%时的translateX的值可以这样理解:由于DEMO中有25个帧,为了显示最后一个帧你需要移动.bg-layer层-100% * (25 – 1) = -96%。接着需要使最后一帧居中显示,就需要额外的调整-2%,所以最后translateX()的值为-98%。

当用户点击了触发按钮a.cd-modal-trigger时,.cd-transition-layer会被添加.visible class类使其可见,同时通过.opening class类来触发喷洒墨水动画。

.cd-transition-layer.visible {  opacity: 1;  visibility: visible;}.cd-transition-layer.opening .bg-layer {  animation: cd-sprite 0.8s steps(24);  animation-fill-mode: forwards;}

JavaScript

DEMO中在用户点击触发按钮和关闭模态窗口按钮时通过jQuery来添加和移除相应的class类。

另外,还使用jQuery来调整png图片的比例使它们不至于变形。在样式文件中,.bg-layer的每一帧的宽度和高度都被设置为和视口一样大小。但是用户显示器的视口可能会是不同的比例,这会导致某些帧不能完全显示。

setLayerDimensions()函数正是用于修正这些问题的。

var frameProportion = 1.78, //png frame aspect ratio  frames = 25, //number of png frames  resize = false;  //set transitionBackground dimentionssetLayerDimensions();$(window).on('resize', function(){  if( !resize ) {    resize = true;    (!window.requestAnimationFrame)                         ? setTimeout(setLayerDimensions, 300)                         : window.requestAnimationFrame(setLayerDimensions);  }});  function setLayerDimensions() {  var windowWidth = $(window).width(),    windowHeight = $(window).height(),    layerHeight, layerWidth;    if( windowWidth/windowHeight > frameProportion ) {    layerWidth = windowWidth;    layerHeight = layerWidth/frameProportion;  } else {    layerHeight = windowHeight;    layerWidth = layerHeight*frameProportion;  }    transitionBackground.css({    'width': layerWidth*frames+'px',    'height': layerHeight+'px',  });    resize = false;}

来源:jQuery之家

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),