2015-04-02 184 views
1

我需要在div上製作動畫邊框。我想出瞭如何用canvas重疊和一些javascript來實現,但現在它超級笨重。無論如何用CSS動畫來完成同樣的動畫?請看這個jsfiddle http://jsfiddle.net/L8ov2fk0/7/CSS動畫邊框

編輯:很多偉大的答案!有些我發現在這方面工作而其他方面沒有。我一直遇到的問題是,我的小提琴中的確切動畫是客戶想要的,我從來沒有能夠調整我發現的任何東西,甚至接近於此。

而且我的代碼

HTML

<div id="buttona" class="button" >Button A</div> 
<div id="buttonb" class="button" >Button B</div> 

<canvas id="bordera"></canvas> 
<canvas id="borderb"></canvas> 

CSS

.button{ 
    padding:10px; 
    border:1px solid red; 
} 
#buttona{ 
    position:absolute; 
} 

#buttonb{ 
    position: absolute; 
    left:100px; 
} 

#bordera{ 
    position:absolute; 
    width:81px; 
    height:40px; 
    pointer-events:none; 
} 

#borderb{ 
    position: absolute; 
    left:100px; 
    width:80px; 
    height:40px; 
    pointer-events:none; 
} 

的Javascript

$('body').on('click', '#buttona', function(){ 
    DrawButtonBorder("bordera"); 
}); 

$('body').on('click', '#buttonb', function(){ 
    DrawButtonBorder("borderb"); 
}); 



/*animated borders*/ 
function Point(x, y, width, height, half) { 
    this.x = x; 
    this.y = y; 
    this.width = width; 
    this.height = height; 
    this.half = half; 
} 

Point.prototype.increment = function() { 
    if (this.half == "upper") { 
     if (this.y > 0 && this.x == 0) 
      this.y--; 
     else if (this.y == 0 && this.x < this.width) 
      this.x++; 
     else if (this.y < this.height/2 && this.x == this.width) 
      this.y++; 
    } 
    else { 
     if (this.y < this.height && this.x == 0) 
      this.y++; 
     else if (this.y == this.height && this.x < this.width) 
      this.x++; 
     else if (this.y > this.height/2 && this.x == this.width) 
      this.y--; 
    } 
} 

animatedBorder = null; 
borderDrawn = ""; 
function DrawButtonBorder(id) { 
    if (borderDrawn != id) { 
     borderDrawn = id; 
     CancelButtonBorder(); 
     ClearButtonBorder("bordera"); 
     ClearButtonBorder("borderb"); 

     var speed = 1; 
     var canvas = document.getElementById(id); 
     var context = canvas.getContext('2d'); 

     var style = getComputedStyle(canvas); 

     var width = parseInt(style.width.replace("px", "")); 
     width = parseInt(width/speed); 

     var height = parseInt(style.height.replace("px", "")); 
     height = parseInt(height/speed); 

     context.canvas.width = width; 
     context.canvas.height = height; 

     var middle = parseInt(height/2); 
     var a = new Point(0, middle, width, height, "upper"); 
     var b = new Point(0, middle, width, height, "lower"); 

     function draw() { 

      //upper half 
      context.strokeStyle = '#D7A95A'; 
      context.moveTo(a.x, a.y); 
      a.increment(); 
      context.lineTo(a.x, a.y); 
      context.stroke(); 

      //lower half 
      context.strokeStyle = '#D7A95A'; 
      context.moveTo(b.x, b.y); 
      b.increment(); 
      context.lineTo(b.x, b.y); 
      context.stroke(); 


      if (a.y > middle && b.y < middle) 
       return; 
      animatedBorder = requestAnimFrame(draw); 
     } 
     draw(); 
    } 
} 

function ClearButtonBorder(id) { 
    var canvas = document.getElementById(id); 
    var context = canvas.getContext('2d'); 
    context.clearRect(0,0,context.canvas.width, context.canvas.height); 
} 

function CancelButtonBorder() { 
    cancelAnimFrame(animatedBorder); 
} 

var requestAnimFrame = window.requestAnimationFrame || 
         window.webkitRequestAnimationFrame || 
         window.mozRequestAnimationFrame || 
         function (callback) { 
          window.setTimeout(callback, 1000/60); 
         }; 

var cancelAnimFrame = window.cancelAnimationFrame || 
         window.webkitCancelAnimationFrame || 
         window.mozCancelAnimationFrame; 
+1

不要以爲一個CSS動畫像這是可能的,但你可以創建一個比你的按鈕大一個像素的動畫gif(假設他們'靜態大小),你點擊可見 – 2015-04-02 16:15:38

+0

雖然可以用CSS畫一條線 - 在svg上使用stroke-dasharray和stroke-dashoffset。 – Shikkediel 2015-04-02 16:17:34

+1

這是一個答案的提示? http://codepen.io/gc-nomade/pen/IGliC – 2015-04-02 16:19:02

回答

1

創建一個div元素作爲邊界,將其放置在有關元素你想要動畫邊框發生。

.border{ 
    height: 10px; //Or whatever height for thickness 
    background-color: black; //Or whichever color 
    visibility: hidden; //So that it takes up space 
} 

當按鈕被點擊這個類添加到邊界元素並把它的知名度可見

.extend{ 
    animation: expand 3s; 
    -webkit-animation: expand 3s; 
} 

包括在CSS

/* Chrome, Safari, Opera */ 
@-webkit-keyframes expand { 
    from {width: 0%;} 
    to {width: 100%;} 
} 

/* Chrome, Safari, Opera */ 
@keyframes expand { 
    from {width: 0%;} 
    to {width: 100%;} 
} 
1

你可以用border-這個動作圖像css風格:

Here is the Example Fiddle

Another Example

#myDIV { 
    border:15px solid transparent; 
    width:250px; 
    padding:10px 20px; 
    border-image:url(http://www.w3schools.com/cssref/border.png) 30 30 stretch; 
    transition: .2s; 
} 
#myDIV:hover{ 
    border-image:url(http://www.w3schools.com/cssref/border.png) 0 0 stretch; 
} 
2

你可以使用背景圖片或漸變和背景大小。

.button { 
 
    padding:0.25em; 
 
    display:inline-block; 
 
    border:none; 
 
    background-color:; 
 
    background: 
 
    linear-gradient(to top,red,red) 0 100%, 
 
    linear-gradient(to right,red,red)100% bottom, 
 
    linear-gradient(to top,red,red) bottom right , 
 
    linear-gradient(to top,red,red) top right lightgray; 
 
    background-size: 3px 0%, 0% 3px,3px 0%, 0% 3px; 
 
    background-repeat:no-repeat,no-repeat,no-repeat,no-repeat; 
 
    transition:1s; 
 
    } 
 
.button:hover { 
 
    background-size: 3px 100%, 100% 3px,3px 100%, 100% 3px; 
 
<button id="buttona" class="button" >Button A</button> 
 
<button id="buttonb" class="button" >Button B</button>

或動畫超過背景從陰影製成

.button { 
 
    padding:0.25em; 
 
    display:inline-block; 
 
    border:none; 
 
    background-color:; 
 
    background: 
 
    linear-gradient(to top,red,red) 0 100%, 
 
    linear-gradient(to right,red,red)100% bottom, 
 
    linear-gradient(to top,red,red) bottom right , 
 
    linear-gradient(to top,red,red) top right lightgray; 
 
    background-size: 3px 100%, 100% 3px,3px 100%, 100% 3px; 
 
    background-repeat:no-repeat,no-repeat,no-repeat,no-repeat; 
 
    box-shadow:inset 0 0 0 0 lightgray; 
 
    transition:1s; 
 
    } 
 
.button:hover { 
 
    box-shadow:inset 5em 2em 0 0 lightgray
<button id="buttona" class="button" >Button A</button> 
 
<button id="buttonb" class="button" >Button B</button>

其他CSS例子:http://codepen.io/gc-nomade/pen/IGliChttp://codepen.io/gc-nomade/pen/bhxALhttp://codepen.io/gc-nomade/pen/pKwby

1

,如果你把你的DIV爲實際<button>元素,你可以用:focus僞類實現這個效果

例子:http://codepen.io/anon/pen/QwPxYm

<button id="buttona" class="button">Button A</button> 
<button id="buttonb" class="button">Button B</button> 

CSS

.button { 
    padding:10px; 
    background: none; 
    outline: none; 
    border:1px solid red; 
} 

#buttona, #buttonb { 
    position:absolute; 
} 

#buttonb { 
    left:100px; 
} 

.button:after { 
    content: ""; 
    position: absolute; 
    bottom: -4px; 
    left: 0; 
    display: block; 
    width: 0; 
    height: 0; 
    border-top: 1px #ccc solid; 
    transition: width 3s; 
} 

.button:focus:after { 
    width: 100%; 
}