js+css炫酷3d立体卡通文字特效

这是一款效果使用纯js和CSS3制作的炫酷3D立体卡通文字特效。该特效通过js代码来克隆字母的内容,并通过 CSS text-shadow属性和transform属性将字母渲染为3D立体效果。

使用方法

HTML结构

该3D立体文字特效的HTML代码非常简单,使用一个<div>l;来包裹文字即可。

<div class="wrapper">
  <div class="content">
    Alphabet Magnet Text Styling
  </div>
</div>           
                
CSS样式

该3D立体文字特效的CSS代码如下:

.content {
  font-family: 'Rubik';
  font-size: 6rem;
  font-weight: 900;
  letter-spacing: .04em;
  display: block;
  word-spacing: 3rem;
  line-height: 1.4;
  text-transform: uppercase;
}
.content div {
  display: inline;
}
.content span {
  display: inline-block;
}
.content span:hover {
  animation: wobble 200ms;
}
.content span:nth-child(1n) {
  color: #F18829;
  text-shadow: #ef7b11 1px 1px, #f7bd89 -1px -1px, #f37841 -2px -2px 6px, #f49d4f -2px -2px, #f49d4f -1px -2px, #f49d4f -1px -3px, #f49d4f -2px -4px, #f49d4f -2px -5px, #f49d4f -3px -6px, #F18829 -4px -7px, rgba(0, 0, 5, 0.4) 3px 4px 5px, rgba(0, 0, 5, 0.2) -3px -4px 5px;
  transform: rotate(-3deg);
}
.content span:nth-child(2n) {
  color: #00B9ED;
  text-shadow: #00a5d4 1px 1px, #54d9ff -1px -1px, #08f2ff -2px -2px 6px, #17ccff -2px -2px, #17ccff -1px -2px, #17ccff -1px -3px, #17ccff -2px -4px, #17ccff -2px -5px, #17ccff -3px -6px, #00B9ED -4px -7px, rgba(0, 0, 5, 0.4) 3px 4px 5px, rgba(0, 0, 5, 0.2) -3px -4px 5px;
  transform: rotate(3deg) translateY(4%);
}
.content span:nth-child(3n) {
  color: #ED5053;
  text-shadow: #eb393c 1px 1px, #f7acae -1px -1px, #ef6780 -2px -2px 6px, #f17577 -2px -2px, #f17577 -1px -2px, #f17577 -1px -3px, #f17577 -2px -4px, #f17577 -2px -5px, #f17577 -3px -6px, #ED5053 -4px -7px, rgba(0, 0, 5, 0.4) 3px 4px 5px, rgba(0, 0, 5, 0.2) -3px -4px 5px;
  transform: rotate(-3deg);
}
.content span:nth-child(4n) {
  color: #00AF4F;
  text-shadow: #009643 1px 1px, #16ff7f -1px -1px, #00c939 -2px -2px 6px, #00d861 -2px -2px, #00d861 -1px -2px, #00d861 -1px -3px, #00d861 -2px -4px, #00d861 -2px -5px, #00d861 -3px -6px, #00AF4F -4px -7px, rgba(0, 0, 5, 0.4) 3px 4px 5px, rgba(0, 0, 5, 0.2) -3px -4px 5px;
  transform: rotate(-2deg);
}
.content span:nth-child(5n) {
  color: #8E509F;
  text-shadow: #7f478e 1px 1px, #ba8fc6 -1px -1px, #8e5cad -2px -2px 6px, #a266b2 -2px -2px, #a266b2 -1px -2px, #a266b2 -1px -3px, #a266b2 -2px -4px, #a266b2 -2px -5px, #a266b2 -3px -6px, #8E509F -4px -7px, rgba(0, 0, 5, 0.4) 3px 4px 5px, rgba(0, 0, 5, 0.2) -3px -4px 5px;
  transform: rotate(3deg) translateY(-2%);
}
.content span:nth-child(6n) {
  color: #F9DE00;
  text-shadow: #e0c700 1px 1px, #ffee60 -1px -1px, #ffbe14 -2px -2px 6px, #ffe723 -2px -2px, #ffe723 -1px -2px, #ffe723 -1px -3px, #ffe723 -2px -4px, #ffe723 -2px -5px, #ffe723 -3px -6px, #F9DE00 -4px -7px, rgba(0, 0, 5, 0.4) 3px 4px 5px, rgba(0, 0, 5, 0.2) -3px -4px 5px;
  transform: rotate(5deg) translateY(1%);
}

@keyframes wobble {
  50% {
    transform: translate(2%, 2%);
  }
}            
                
JavaScript

JS代码的左右主要是克隆字母的内容。代码如下:

'use strict';
var container = document.querySelector('.content');
var content = container.innerText;

function formatContent(content, container) {
  var contentArray = content.split(' ');
  var formattedContent = document.createElement('div');
  contentArray.map(function (word) {
    formattedContent.appendChild(createWord(word));
  });
  console.log(contentArray);

  container.replaceChild(formattedContent, container.firstChild);
};

function createWord(characters) {
  var word = document.createElement('div');
  var wordArray = characters.split('');
  wordArray.map(function (char) {
    word.appendChild(formatCharacter(char));
  });
  word.appendChild(formatCharacter(' '));
  return word;
}

function formatCharacter(text) {
  var text = text === ' ' ? ' ' : text;
  var character = document.createElement('span');
  character.innerHTML = text;
  return character;
}

formatContent(content, container);                  
                

在线预览    源码下载

爱编程-编程爱好者经验分享平台
版权所有 爱编程 © Copyright 2012. All Rights Reserved.
闽ICP备12017094号-3