1. 程式人生 > >Coder往事之: 一些炫酷的特效 for web 前端 (一)

Coder往事之: 一些炫酷的特效 for web 前端 (一)

群星環繞

群星環繞

html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>星星環繞</title>
        <link rel="stylesheet" href="css/index.css" />
        <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
        <script
type="text/javascript" src="js/index.js">
</script> </head> <body> <!-- 作者:[email protected] 時間:2018-01-09 描述:群星環繞 --> <center> <h1>我是一個<span>被施了魔法的</span>皮皮怪!</h1> <button
class="Shine">
Hover Me</button> <button class="Shine last">Hover Me!!</button> </center> </body> </html>

css

.Shine {
  background: #3e5771;
  color: white;
  border: none;
  padding: 16px 36px;
  font-weight: normal;
  border-radius: 3
px
; transition: all 0.25s ease; box-shadow: 0 38px 32px -23px black; margin: 0 1em 1em; }
.Shine:hover { background: #2c3e50; color: rgba(255, 255, 255, 0.2); }

Js

/*
        Author:[email protected]
        Time:2018-01-09
        Des:群星環繞
        LastModify:2018年1月9日11:17:01
 */
$(function() {

  // 預設是不同程度的透明白色閃爍
  $(".Shine:first").sparkleh();

  // rainbow用來產生隨機顏色
  // count決定閃閃發光的數量
  // overlap決定閃爍點的移動,不過要小心其他的dom事件.
  $(".Shine:last").sparkleh({
    color: "rainbow",
    count: 100,
    overlap: 10
  });

  // 在這裡建立了Fuscia閃爍
  $("h1").sparkleh({
    count: 80,
    color: "#ff0080"
  });

  $("p").sparkleh({
    count: 20,
    color: "#00ff00"
  });

  // color也可以是陣列
  // 對於image,需要完全載入才能設定
  // canvas的高度或者寬度 
  $("#image").imagesLoaded( function() {
    $(".img").sparkleh({
      count: 25,
      color: ["#00afec","#fb6f4a","#fdfec5"]
    });
  });

});

$.fn.sparkleh = function( options ) {

  return this.each( function(k,v) {

    var $this = $(v).css("position","relative");

    var settings = $.extend({
      width: $this.outerWidth(),
      height: $this.outerHeight(),
      color: "#FFFFFF",
      count: 30,
      overlap: 0
    }, options );

    var sparkle = new Sparkle( $this, settings );

    $this.on({
      "mouseover focus" : function(e) {
        sparkle.over();
      },
      "mouseout blur" : function(e) {
        sparkle.out();
      }
    });

  });

}

function Sparkle( $parent, options ) {
  this.options = options;
  this.init( $parent );
}

Sparkle.prototype = {

  "init" : function( $parent ) {

    var _this = this;

    this.$canvas = 
      $("<canvas>")
        .addClass("sparkle-canvas")
        .css({
          position: "absolute",
          top: "-"+_this.options.overlap+"px",
          left: "-"+_this.options.overlap+"px",
          "pointer-events": "none"
        })
        .appendTo($parent);

    this.canvas = this.$canvas[0];
    this.context = this.canvas.getContext("2d");
    this.sprite = new Image();

    this.canvas.width = this.options.width + ( this.options.overlap * 2);
    this.canvas.height = this.options.height + ( this.options.overlap * 2);

    this.sprites = [0,6,13,20];
    this.particles = this.createSparkles( this.canvas.width , this.canvas.height );

    this.anim = null;
    this.fade = false;

  },

  "createSparkles" : function( w , h ) {

    var holder = [];

    for( var i = 0; i < this.options.count; i++ ) {

      var color = this.options.color;

      if( this.options.color == "rainbow" ) {
        color = '#'+Math.floor(Math.random()*16777215).toString(16);
      } else if( $.type(this.options.color) === "array" ) {
        color = this.options.color[ Math.floor(Math.random()*this.options.color.length) ];
      }

      holder[i] = {
        position: {
          x: Math.floor(Math.random()*w),
          y: Math.floor(Math.random()*h)
        },
        style: this.sprites[ Math.floor(Math.random()*4) ],
        delta: {
          x: Math.floor(Math.random() * 1000) - 500,
          y: Math.floor(Math.random() * 1000) - 500
        },
        size: parseFloat((Math.random()*2).toFixed(2)),
        color: color
      };

    }

    return holder;

  },

  "draw" : function( time, fade ) {

    var ctx = this.context;
    var img = this.sprite;
        img.src = this.datauri;

    ctx.clearRect( 0, 0, this.canvas.width, this.canvas.height );

    for( var i = 0; i < this.options.count; i++ ) {

      var derpicle = this.particles[i];
      var modulus = Math.floor(Math.random()*7);

      if( Math.floor(time) % modulus === 0 ) {
        derpicle.style = this.sprites[ Math.floor(Math.random()*4) ];
      }

      ctx.save();
      ctx.globalAlpha = derpicle.opacity;
      ctx.drawImage(img, derpicle.style, 0, 7, 7, derpicle.position.x, derpicle.position.y, 7, 7);

      if( this.options.color ) {  

        ctx.globalCompositeOperation = "source-atop";
        ctx.globalAlpha = 0.5;
        ctx.fillStyle = derpicle.color;
        ctx.fillRect(derpicle.position.x, derpicle.position.y, 7, 7);

      }

      ctx.restore();

    }

  },

  "update" : function() {

     var _this = this;

     this.anim = window.requestAnimationFrame( function(time) {

       for( var i = 0; i < _this.options.count; i++ ) {

         var u = _this.particles[i];

         var randX = ( Math.random() > Math.random()*2 );
         var randY = ( Math.random() > Math.random()*3 );

         if( randX ) {
           u.position.x += (u.delta.x / 1500); 
         }        

         if( !randY ) {
           u.position.y -= (u.delta.y / 800);         
         }

         if( u.position.x > _this.canvas.width ) {
           u.position.x = -7;
         } else if ( u.position.x < -7 ) {
           u.position.x = _this.canvas.width; 
         }

         if( u.position.y > _this.canvas.height ) {
           u.position.y = -7;
           u.position.x = Math.floor(Math.random()*_this.canvas.width);
         } else if ( u.position.y < -7 ) {
           u.position.y = _this.canvas.height; 
           u.position.x = Math.floor(Math.random()*_this.canvas.width);
         }

         if( _this.fade ) {
           u.opacity -= 0.02;
         } else {
           u.opacity -= 0.005;
         }

         if( u.opacity <= 0 ) {
           u.opacity = ( _this.fade ) ? 0 : 1;
         }

       }

       _this.draw( time );

       if( _this.fade ) {
         _this.fadeCount -= 1;
         if( _this.fadeCount < 0 ) {
           window.cancelAnimationFrame( _this.anim );
         } else {
           _this.update(); 
         }
       } else {
         _this.update();
       }

     });

  },

  "cancel" : function() {

    this.fadeCount = 100;

  },

  "over" : function() {

    window.cancelAnimationFrame( this.anim );

    for( var i = 0; i < this.options.count; i++ ) {
      this.particles[i].opacity = Math.random();
    }

    this.fade = false;
    this.update();

  },

  "out" : function() {

    this.fade = true;
    this.cancel();

  },

  "datauri" : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABsAAAAHCAYAAAD5wDa1AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbW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IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+jzOsUQAAANhJREFUeNqsks0KhCAUhW/Sz6pFSc1AD9HL+OBFbdsVOKWLajH9EE7GFBEjOMxcUNHD8dxPBCEE/DKyLGMqraoqcd4j0ChpUmlBEGCFRBzH2dbj5JycJAn90CEpy1J2SK4apVSM4yiKonhePYwxMU2TaJrm8BpykpWmKQ3D8FbX9SOO4/tOhDEG0zRhGAZo2xaiKDLyPGeSyPM8sCxr868+WC/mvu9j13XBtm1ACME8z7AsC/R9r0fGOf+arOu6jUwS7l6tT/B+xo+aDFRo5BykHfav3/gSYAAtIdQ1IT0puAAAAABJRU5ErkJggg=="

}; 

// $('img.photo',this).imagesLoaded(myFunction)
// 因為.load()不適用於快取的影象,所以所有影象載入完成後執行回撥.

// 回撥函式傳遞最後一個影象作為引數載入,集合為`this`

$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len   = elems.length,
      blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";

  elems.bind('load.imgloaded',function(){
      if (--len <= 0 && this.src !== blank){ 
        elems.unbind('load.imgloaded');
        callback.call(elems,this); 
      }
  }).each(function(){
     // 快取的影象有時不會觸發負載,所以我們重置src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        this.src = blank;
        this.src = src;
     }  
  }); 

  return this;
};

雪花飄落

這裡寫圖片描述

html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>雪花飄落</title>
        <link rel="apple-touch-icon" href="images/apple-touch-icon.png" />
        <!-- The leaves.css file animates the leaves -->
        <link rel="stylesheet" href="css/SnowFall.css" type="text/css" media="screen" charset="utf-8">
        <!-- The leaves.js file creates the leaves -->
        <script src="js/SnowFall.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        <!--
            作者:[email protected]
            時間:2018-01-09
            描述:雪花飄落
        -->
        <div id="container">
            <!-- The container is dynamically populated using the init function in leaves.js -->
            <!-- Its dimensions and position are defined using its id selector in leaves.css -->
            <div id="leafContainer"></div>
            <!-- its appearance, dimensions, and position are defined using its id selector in leaves.css -->
        </div>
    </body>
</html>

css

/*
        Author:[email protected]
        Time:2018-01-09
        Des:雪花飄落
        LastModify: 2018年1月9日12:07:03
 */

body
{
    background-color: #fff;
}

#container {
    position: relative;
    height: 90vh;
    width: 90vw;
    margin: 10px auto;
    overflow: hidden;
    border: 4px solid #ccc;
}

/* Defines the position and dimensions of the leafContainer div */
#leafContainer 
{
    position: absolute;
    width: 100%;
    height: 100%;
}

/* Sets the color of the "Dino's Gardening Service" message */
em 
{
    font-weight: bold;
    font-style: normal;
}

/* This CSS rule is applied to all div elements in the leafContainer div.
   It styles and animates each leafDiv.
*/
#leafContainer > div 
{
    position: absolute;
    width: 100px;
    height: 100px;

    /* We use the following properties to apply the fade and drop animations to each leaf.
       Each of these properties takes two values. These values respectively match a setting
       for fade and drop.
    */
    -webkit-animation-iteration-count: infinite, infinite;
    -webkit-animation-direction: normal, normal;
    -webkit-animation-timing-function: linear, ease-in;
}

/* This CSS rule is applied to all img elements directly inside div elements which are
   directly inside the leafContainer div. In other words, it matches the 'img' elements
   inside the leafDivs which are created in the createALeaf() function.
*/
#leafContainer > div > img {
     position: absolute;
     width: 50px;
     height: 50px;

    /* We use the following properties to adjust the clockwiseSpin or counterclockwiseSpinAndFlip
       animations on each leaf.
       The createALeaf function in the Leaves.js file determines whether a leaf has the 
       clockwiseSpin or counterclockwiseSpinAndFlip animation.
    */
     -webkit-animation-iteration-count: infinite;
     -webkit-animation-direction: alternate;
     -webkit-animation-timing-function: ease-in-out;
     -webkit-transform-origin: 50% -100%;
}


/* Hides a leaf towards the very end of the animation */
@-webkit-keyframes fade
{
    /* Show a leaf while into or below 95 percent of the animation and hide it, otherwise */
    0%   { opacity: 1; }
    95%  { opacity: 1; }
    100% { opacity: 0; }
}


/* Makes a leaf fall from -300 to 600 pixels in the y-axis */
@-webkit-keyframes drop
{
    /* Move a leaf to -300 pixels in the y-axis at the start of the animation */
    0%   { -webkit-transform: translate(0px, -50px); }
    /* Move a leaf to 600 pixels in the y-axis at the end of the animation */
    100% { -webkit-transform: translate(0px, 850px); }
}

/* Rotates a leaf from -50 to 50 degrees in 2D space */
@-webkit-keyframes clockwiseSpin
{
    /* Rotate a leaf by -50 degrees in 2D space at the start of the animation */
    0%   { -webkit-transform: rotate(-50deg); }
    /*  Rotate a leaf by 50 degrees in 2D space at the end of the animation */
    100% { -webkit-transform: rotate(50deg); }
}


/* Flips a leaf and rotates it from 50 to -50 degrees in 2D space */
@-webkit-keyframes counterclockwiseSpinAndFlip 
{
    /* Flip a leaf and rotate it by 50 degrees in 2D space at the start of the animation */
    0%   { -webkit-transform: scale(-1, 1) rotate(50deg); }
    /* Flip a leaf and rotate it by -50 degrees in 2D space at the end of the animation */
    100% { -webkit-transform: scale(-1, 1) rotate(-50deg); }
}

Js

/*
        Author:[email protected]
        Time:2018-01-09
        Des:雪花飄落
        LastModify: 2018年1月9日12:07:03
 */

const NUMBER_OF_LEAVES = 60;
/* 
    Called when the "Falling Leaves" page is completely loaded.
*/
function init()
{
    /* Get a reference to the element that will contain the leaves */
    var container = document.getElementById('leafContainer');
    /* Fill the empty container with new leaves */
    for (var i = 0; i < NUMBER_OF_LEAVES; i++) 
    {
        container.appendChild(createALeaf());
    }
}
/*
    Receives the lowest and highest values of a range and
    returns a random integer that falls within that range.
*/
function randomInteger(low, high)
{
    return low + Math.floor(Math.random() * (high - low));
}
/*
   Receives the lowest and highest values of a range and
   returns a random float that falls within that range.
*/
function randomFloat(low, high)
{
    return low + Math.random() * (high - low);
}


/*
    Receives a number and returns its CSS pixel value.
*/
function pixelValue(value)
{
    return value + 'vw';
}


/*
    Returns a duration value for the falling animation.
*/

function durationValue(value)
{
    return value + 's';
}


/*
    Uses an img element to create each leaf. "Leaves.css" implements two spin 
    animations for the leaves: clockwiseSpin and counterclockwiseSpinAndFlip. This
    function determines which of these spin animations should be applied to each leaf.

*/
function createALeaf()
{
    /* Start by creating a wrapper div, and an empty img element */
    var leafDiv = document.createElement('div');
    var image = document.createElement('img');

    /* Randomly choose a leaf image and assign it to the newly created element */
    //image.src = 'images/realLeaf' + randomInteger(1, 5) + '.png';
    image.src = 'img/Snow.png';

    leafDiv.style.top = "-50px";

    /* Position the leaf at a random location along the screen */
    leafDiv.style.left = pixelValue(randomInteger(0, 90));

    /* Randomly choose a spin animation */
    var spinAnimationName = (Math.random() < 0.5) ? 'clockwiseSpin' : 'counterclockwiseSpinAndFlip';

    /* Set the -webkit-animation-name property with these values */
    leafDiv.style.webkitAnimationName = 'fade, drop';
    image.style.webkitAnimationName = spinAnimationName;

    /* Figure out a random duration for the fade and drop animations */
    var fadeAndDropDuration = durationValue(randomFloat(5, 11));

    /* Figure out another random duration for the spin animation */
    var spinDuration = durationValue(randomFloat(4, 8));
    /* Set the -webkit-animation-duration property with these values */
    leafDiv.style.webkitAnimationDuration = fadeAndDropDuration + ', ' + fadeAndDropDuration;

    var leafDelay = durationValue(randomFloat(0, 5));
    leafDiv.style.webkitAnimationDelay = leafDelay + ', ' + leafDelay;

    image.style.webkitAnimationDuration = spinDuration;

    // add the <img> to the <div>
    leafDiv.appendChild(image);

    /* Return this img element so it can be added to the document */
    return leafDiv;
}


/* Calls the init function when the "Falling Leaves" page is full loaded */
window.addEventListener('load', init, false);

閃耀按鈕

閃耀按鈕

html

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title>閃光按鈕</title>
        <link rel="stylesheet" href="css/ShineBtn.css" />
    </head>
    <body id="radioactiveButtonsPage" class="chrome windows">
        <div class="wall-of-buttons">
            <a class="large green button">路飛</a>
            <a class="large blue button">黑崎一護</a>
            <a class="large magenta button">白鬍子</a>
            <a class="large green button">朽木露琪亞</a>
            <a class="large red button">藍染</a>
            <a class="large magenta button">井上織姬</a>
            <br />

            <a class="large orange button">野比大雄</a>
            <a class="large magenta button">剛田武胖虎</a>
            <a class="large green button">骨川小夫</a>
            <a class="large orangellow button">源靜香</a>
            <a class="large blue button">哆啦A夢</a>
            <a class="large red button">夏目友人帳</a>
            <a class="large blue button">娘口三三</a>
            <br />

            <a class="large magenta button">阪本</a>
            <a class="large orangellow button">出木杉</a>
            <a class="large red button">哆啦小子</a>
            <a class="large orange button">哆啦王</a>
            <a class="large green button">哆啦尼可夫</a>
            <a class="large orangellow button">哆啦利鈕</a>
            <a class="large red button">哆啦梅度三世</a>

            <a class="large blue button">耶魯馬他哆啦</a>
            <a class="large orangellow button">御阪美琴</a>
            <a class="large blue button">利威爾·阿克曼</a>
            <a class="large red button">叶音竹</a>
            <a class="large orange button">蕭炎</a>
            <a class="large orangellow button">美少女戰士</a>
        </div>
    </body>

</html>

Css


body {
                background: #333;
                text-shadow: 0 1px 1px rgba(0, 0, 0, .5);
            }

            @-webkit-keyframes bigAssButtonPulse {
                from {
                    background-color: #749a02;
                    -webkit-box-shadow: 0 0 25px #333;
                }
                50% {
                    background-color: #91bd09;
                    -webkit-box-shadow: 0 0 50px #91bd09;
                }
                to {
                    background-color: #749a02;
                    -webkit-box-shadow: 0 0 25px #333;
                }
            }

            @-webkit-keyframes greenPulse {
                from {
                    background-color: #749a02;
                    -webkit-box-shadow: 0 0 9px #333;
                }
                50% {
                    background-color: #91bd09;
                    -webkit-box-shadow: 0 0 18px #91bd09;
                }
                to {
                    background-color: #749a02;
                    -webkit-box-shadow: 0 0 9px #333;
                }
            }

            @-webkit-keyframes bluePulse {
                from {
                    background-color: #007d9a;
                    -webkit-box-shadow: 0 0 9px #333;
                }
                50% {
                    background-color: #2daebf;
                    -webkit-box-shadow: 0 0 18px #2daebf;
                }
                to {
                    background-color: #007d9a;
                    -webkit-box-shadow: 0 0 9px #333;
                }
            }

            @-webkit-keyframes redPulse {
                from {
                    background-color: #bc330d;
                    -webkit-box-shadow: 0 0 9px #333;
                }
                50% {
                    background-color: #e33100;
                    -webkit-box-shadow: 0 0 18px #e33100;
                }
                to {
                    background-color: #bc330d;
                    -webkit-box-shadow: 0 0 9px #333;
                }
            }

            @-webkit-keyframes magentaPulse {
                from {
                    background-color: #630030;
                    -webkit-box-shadow: 0 0 9px #333;
                }
                50% {
                    background-color: #a9014b;
                    -webkit-box-shadow: 0 0 18px #a9014b;
                }
                to {
                    background-color: #630030;
                    -webkit-box-shadow: 0 0 9px #333;
                }
            }

            @-webkit-keyframes orangePulse {
                from {