search
HomeWeb Front-endHTML TutorialHow to use Vue+CSS3 to create interactive effects

We know that interactive effects or special effects will definitely be used when doing projects. I once developed a project in which Guo has been using Vue. In terms of developing the technology stack, I used Vue+CSS3. In the process, I found that vue+css3 development special effects are very useful. It’s easy to use. Today I will bring you such a tutorial.

1. Although the code in the article is very simple and not difficult to understand, it is recommended that you read it while writing to avoid confusion.
2. The small examples mentioned in the article are very basic. You can expand or modify them based on your own ideas, which may have unexpected effects. When I write this type of article, I also want to teach people how to fish, not teach them how to fish!
3. These examples are taken from my own daily practice projects, and the code has been mentioned on github (vue-demos). Welcome everyone to star.

2. Opening animation

Principle analysis

Speaking of principle analysis, there is actually nothing to analyze. It is to replace the text when the page is in the following state. Lose. As for seeing the font shrinking into a ball, it is the control effect of the letter-spacing css attribute. Font blur is the control effect of the filter: blur() css attribute! You can see that there are gradual changes, which is the effect of CSS3 animation.

Let’s briefly analyze the steps of this animation. As you can see from the bottom, this animation has a total of 8 steps.

Now it is clear. We want to start changing the text at the moment shown in the picture below, that is, two seconds after the page is loaded, and the animation is executed twice before starting to change the text. Then change the text every two seconds until the end!

The codes of vue and javascript are given below to see which method is simpler!

vue method

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Title</title> </head> <style>     body{         background: #ccc;     }     h1 {         color: white;         text-transform: uppercase;         margin-top: 100px;         text-align: center;         font-size: 6rem;         line-height: 1;         animation: letterspacing 1s 7 alternate ease-in-out;         display: block;         letter-spacing: .5rem;     }     @keyframes letterspacing {         0% {             letter-spacing: -72px;             filter: blur(20px);         }         40% {             filter: blur(6px);         }         80% {             letter-spacing: 8px;             filter: blur(0);         }     } </style> <body> <div id="text">     <h1 id="testText">{{testText}}</h1> </div> </body> <script src="vue.min.js"></script> <script type="text/javascript">     new Vue({         el:&#39;#text&#39;,         data:{             nowIndex:0,             testText:&#39;欢迎浏览&#39;         },         mounted(){             let _this=this;             let timer = setInterval(function(){                 _this.nowIndex++;                 switch (_this.nowIndex) {                     case 1:                         _this.testText = &#39;守候的文章&#39;;                         break;                     case 2:                         _this.testText = &#39;愿您浏览愉快&#39;;                         break;                     case 3:                         _this.testText = &#39;学到知识&#39;;                         break;                 }                 if (_this.nowIndex > 3) {                     setTimeout(() => {                         clearInterval(timer);                     }, 2000)                 }             }, 2000)         }     }) </script> </html>

javascript method

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Title</title> </head> <style>     body{         background: #ccc;     }     h1 {         color: white;         text-transform: uppercase;         margin-top: 100px;         text-align: center;         font-size: 6rem;         line-height: 1;         animation: letterspacing 1s 7 alternate ease-in-out;         display: block;         letter-spacing: .5rem;     }     @keyframes letterspacing {         0% {             letter-spacing: -6rem;             filter: blur(1rem);         }         40% {             filter: blur(.3rem);         }         80% {             letter-spacing: .5rem;             filter: blur(0rem);         }     } </style> <body> <div id="text">     <h1 id="欢迎浏览">欢迎浏览</h1> </div> </body> <script>     var oH1=document.querySelector(&#39;h1&#39;),nowIndex=0;     console.log(oH1)     var timer = setInterval(function () {         nowIndex++;         switch (nowIndex) {             case 1:                 oH1.innerHTML = &#39;守候的文章&#39;;                 break;             case 2:                 oH1.innerHTML = &#39;愿您浏览愉快&#39;;                 break;             case 3:                 oH1.innerHTML = &#39;学到知识&#39;;                 break;         }         if (nowIndex > 3) {             setTimeout(() => {                 clearInterval(timer);             }, 2000)         }     }, 2000) </script> </html>

3. Navigation slider

Operation effect

Principle analysis

First of all, the following is the position of the orange slider when the page is initialized

Put the mouse on the second tab. You can see that the orange slider is offset to the right by a tab distance

Put the mouse on the third tab, you can see that the orange slider is offset to the right by the distance of two tabs

If from the first tab to the sixth tab The indices are 0,1,2,3,4,5.

Then the formula of the slider is (index * width of tab). As you can see, the effect that gradually passes is actually the effect of CSS3 transition. Just look at the code below, you will understand it at a glance! The code is as follows:

vue method

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Title</title> </head> <link rel="stylesheet" href="reset.css"> <style>     .nav{         margin: 40px;         position: relative;     } .nav li{     float: left;     width: 100px;     height: 40px;     line-height: 40px;     color: #fff;     text-align: center;     background: #09f;     cursor: pointer; }     .nav span{         position: relative;         z-index: 2;     }     .nav .slider{         position: absolute;         transition: all .5s cubic-bezier(0.4, -0.3, 0.57, 1.38);         width: 100px;         height: 40px;         background: #f90;         top: 0;         left: 0;         z-index: 1;     } </style> <body> <div class="nav clear" id="nav" @mouseleave="nowIndex=0">     <ul>         <li @mouseenter.stop="nowIndex=0"><span>Tab One</span></li>         <li @mouseenter.stop="nowIndex=1"><span>Tab Two</span></li>         <li @mouseenter.stop="nowIndex=2"><span>Tab Three</span></li>         <li @mouseenter.stop="nowIndex=3"><span>Tab four</span></li>         <li @mouseenter.stop="nowIndex=4"><span>Tab five</span></li>         <li @mouseenter.stop="nowIndex=5"><span>Tab six</span></li>     </ul>     <div class="slider" :style="{&#39;transform&#39;:&#39;translate3d(&#39;+nowIndex*100+&#39;px,0,0)&#39;}"></div> </div> </body> <script src="vue.min.js"></script> <script type="text/javascript">    new Vue({        el:&#39;#nav&#39;,        data:{            nowIndex:0        }    }) </script> </html>

javascript method

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Title</title> </head> <link rel="stylesheet" href="reset.css"> <style>     .nav{         position: relative;     } .nav li{     float: left;     width: 100px;     height: 40px;     line-height: 40px;     color: #fff;     text-align: center;     background: #09f;     cursor: pointer; }     .nav span{         position: relative;         z-index: 2;     }     .nav .slider{         position: absolute;         transition: all .5s cubic-bezier(0.4, -0.3, 0.57, 1.38);         width: 100px;         height: 40px;         background: #f90;         top: 0;         left: 0;         z-index: 1;     } </style> <body> <div class="nav clear" id="nav">     <ul>         <li><span>Tab One</span></li>         <li><span>Tab Two</span></li>         <li><span>Tab Three</span></li>         <li><span>Tab four</span></li>         <li><span>Tab five</span></li>         <li><span>Tab six</span></li>     </ul>     <div class="slider"></div> </div> </body> <script type="text/javascript">     var oDiv=document.querySelector("#nav"),oLi=oDiv.querySelectorAll("li"),oSlider=document.querySelector(".slider");     oDiv.addEventListener("mouseleave",function () {         oSlider.style.transform=&#39;translate3d(0,0,0)&#39;;     })     for(var i=0;i<oLi.length;i++){         oLi[i].index=i;         oLi[i].addEventListener("mouseenter",function (e) {             oSlider.style.transform=&#39;translate3d(&#39;+this.index*100+&#39;px,0,0)&#39;;         })     } </script> </html>

4. Carousel image

The blue box is li and the black box is div

Seeing the above, it actually controls the offset of ul (transform:translate3d). The calculation formula is similar to the slider above, index (0|1|2|3)*width of li. The difference is that the offset of ul is a negative number, because ul wants to move to the left, and the slider above wants to move to the right!
When the first picture is taken, the ul offset is set (transform: translate3d(0px, 0px, 0px)).
When the second picture is taken, the ul offset is set (transform: translate3d(-1000px, 0px, 0px)).
When the second picture is taken, the ul offset is set (transform: translate3d(-2000px, 0px, 0px)). By analogy, the offset can be calculated easily!

Maybe everyone is a little confused by what I said, but if you look at the code below, you won’t be confused, because the code is also very simple!

vue way

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Title</title>     <link rel="stylesheet" href="reset.css">     <style>         .slide-img {             width: 1000px;             height: 500px;             overflow: hidden;             position: relative;             margin: 20px auto;         }         ul {             transition: all .5s ease;         }         li {             float: left;         }         .slide-arrow div {             width: 50px;             height: 100px;             position: absolute;             margin: auto;             top: 0;             bottom: 0;             background: url("http://i1.bvimg.com/1949/4d860a3067fab23b.jpg") no-repeat;         }         .arrow-right {             transform: rotate(180deg);             right: 0;         }         .arrow-left {             left: 0;         }         .slide-option{             position: absolute;             bottom: 10px;             width: 100%;             left: 0;             text-align: center;         }         .slide-option span{             display: inline-block;             width: 14px;             height: 14px;             border-radius: 100%;             background: #ccc;             margin: 0 10px;         }         .slide-option .active{             background: #09f;         }     </style> </head> <body> <div class="slide-img clear" id="slide-img">     <!--用tran这个class控制ul是否含有过渡效果,样式已经写好-->     <ul :style="{&#39;width&#39;:(listWidth*list.length)+&#39;px&#39;,&#39;transform&#39;:&#39;translate3d(-&#39;+(listWidth*nowIndex)+&#39;px,0,0)&#39;}">         <!--遍历出来的图片-->         <li v-for="(li,index) in list" :style="{&#39;width&#39;:listWidth+&#39;px&#39;}">             <a href="javascript:;">                 <img  class="slider-img lazy"  src="/static/imghwm/default1.png"  data-src="li"  : / alt="How to use Vue+CSS3 to create interactive effects" >             </a>         </li>     </ul>     <div class="slide-option">         <span v-for="(li,index) in list" :class="{&#39;active&#39;:index===nowIndex}"></span>     </div>     <div class="slide-arrow">         <div class="arrow-left" @click.stop="switchDo(&#39;reduce&#39;)"></div>         <div class="arrow-right" @click.stop="switchDo"></div>     </div> </div> </body> <script src="vue.min.js"></script> <script type="text/javascript">     new Vue({         el: &#39;#slide-img&#39;,         data: {             nowIndex: 0,             listWidth: &#39;1000&#39;,             list: [&#39;./images/timg1.jpg&#39;, &#39;./images/timg2.jpg&#39;, &#39;./images/timg3.jpg&#39;, &#39;./images/timg4.jpg&#39;],             timer:null         },         methods: {             //滑动操作             switchDo(reduce){                 clearInterval(this.timer);                 //根据reduce判断this.nowIndex的增加或者减少!                 if(reduce===&#39;reduce&#39;){                     //如果是第一张,就返回最后一张                     if(this.nowIndex===0){                         this.nowIndex=this.list.length-1;                     }                     else{                         this.nowIndex--;                     }                 }                 else{                     //如果是最后一张,就返回第一张                     if(this.nowIndex===this.list.length-1){                         this.nowIndex=0;                     }                     else{                         this.nowIndex++;                     }                 }                 var _this=this;                 this.timer=setInterval(function () {                     _this.switchDo();                 },4000)             },         },         mounted(){             var _this=this;             this.timer=setInterval(function () {                 _this.switchDo();             },4000)         }     }) </script> </html>

javascript way

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Title</title>     <link rel="stylesheet" href="reset.css">     <style>         .slide-img {             width: 1000px;             height: 500px;             overflow: hidden;             position: relative;             margin: 20px auto;         }         ul {             transition: all .5s ease;         }         li {             float: left;         }         .slide-arrow div {             width: 50px;             height: 100px;             position: absolute;             margin: auto;             top: 0;             bottom: 0;             background: url("http://i1.bvimg.com/1949/4d860a3067fab23b.jpg") no-repeat;         }         .arrow-right {             transform: rotate(180deg);             right: 0;         }         .arrow-left {             left: 0;         }         .slide-option{             position: absolute;             bottom: 10px;             width: 100%;             left: 0;             text-align: center;         }         .slide-option span{             display: inline-block;             width: 14px;             height: 14px;             border-radius: 100%;             background: #ccc;             margin: 0 10px;         }         .slide-option .active{             background: #09f;         }     </style> </head> <body> <div class="slide-img clear" id="slide-img">     <!--用tran这个class控制ul是否含有过渡效果,样式已经写好-->     <ul id="slide-img-ul">         <!--遍历出来的图片-->         <li   style="max-width:90%"><a href="javascript:;"><img  class="slider-img lazy"  src="/static/imghwm/default1.png"  data-src="images/timg1.jpg"   / alt="How to use Vue+CSS3 to create interactive effects" ></a></li>         <li   style="max-width:90%"><a href="javascript:;"><img  class="slider-img lazy"  src="/static/imghwm/default1.png"  data-src="images/timg2.jpg"   / alt="How to use Vue+CSS3 to create interactive effects" ></a></li>         <li   style="max-width:90%"><a href="javascript:;"><img  class="slider-img lazy"  src="/static/imghwm/default1.png"  data-src="images/timg3.jpg"   / alt="How to use Vue+CSS3 to create interactive effects" ></a></li>         <li   style="max-width:90%"><a href="javascript:;"><img  class="slider-img lazy"  src="/static/imghwm/default1.png"  data-src="images/timg4.jpg"   / alt="How to use Vue+CSS3 to create interactive effects" ></a></li>     </ul>     <div class="slide-option">         <span></span>         <span></span>         <span></span>         <span></span>     </div>     <div class="slide-arrow">         <div class="arrow-left"></div>         <div class="arrow-right"></div>     </div> </div> </body> <script type="text/javascript">     window.onload=function () {         var oUl=document.querySelector(&#39;#slide-img-ul&#39;);         var oLi=oUl.querySelectorAll(&#39;li&#39;);         var oSpan=document.querySelector(&#39;.slide-option&#39;).querySelectorAll(&#39;span&#39;);         var oArrowLeft=document.querySelector(&#39;.arrow-left&#39;);         var oArrowRight=document.querySelector(&#39;.arrow-right&#39;);         oUl.style.width=&#39;4000px&#39;;         oArrowLeft.addEventListener(&#39;click&#39;,function () {             switchDo(&#39;reduce&#39;);         })         oArrowRight.addEventListener(&#39;click&#39;,function () {             switchDo();         })         var timer=null,nowIndex=0;         function switchDo(reduce){             clearInterval(timer);             //设置样式             oUl.style.transform=&#39;translate3d(-&#39;+(1000*nowIndex)+&#39;px,0,0)&#39;;             for (var i=0;i<oSpan.length;i++){                 if(i===nowIndex){                     oSpan[i].className=&#39;active&#39;;                 }                 else{                     oSpan[i].className=&#39;&#39;;                 }             }             //根据reduce判断this.nowIndex的增加或者减少!             if(reduce===&#39;reduce&#39;){                 //如果是第一张,就返回最后一张                 if(nowIndex===0){                     nowIndex=oLi.length-1;                 }                 else{                     nowIndex--;                 }             }             else{                 //如果是最后一张,就返回第一张                 if(nowIndex===oLi.length-1){                     nowIndex=0;                 }                 else{                     nowIndex++;                 }             }             timer=setInterval(function () {                 switchDo();             },4000)         }         switchDo();     } </script> </html>

5. Summary

Okay, about the special effects of vue+css3 development, and the use of javascript+css3 That’s it for comparison. I hope these three small examples can help everyone understand how to use vue+css3 to develop special effects. The three small examples I'm talking about today are not meant to give you codes for you to copy and paste, but I hope they can serve as a starting point and expand your thinking! As I said in my previous article, I write the article hoping to play a role in teaching people how to fish, rather than teaching people how to fish! Finally, if you think there is anything I wrote wrong or wrong, or if you have any other suggestions, please point it out! Let everyone learn from each other and make progress together!

I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!


Related reading:

How to create the loading effect of CSS3

##CSS How to convert the encoding

#How to use canvas to create the effect of particle fountain animation

The above is the detailed content of How to use Vue+CSS3 to create interactive effects. For more information, please follow other related articles on the PHP Chinese website!

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
HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

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 Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.