DIV CSS 佈局教程網

jQuery 仿搜狐投票對決動畫:VS 圖示隨比例移動
編輯:JQuery特效代碼     

體驗效果:https://hovertree.com/texiao/jquery/21/

這是一段模仿搜狐投票風格的 jQuery 動畫。頁面載入時 VS 圖示先出現在正中央,接著依投票比例左右移動,位移過程平滑自然,流暢度相當不錯,並相容目前主流瀏覽器。除了視覺效果,實際的計票功能也一併完成。

結構說明

容器 #container 內有三條色帶:#green(正方)、#red(反方)、#white(過場遮罩),以及居中的 #vs 圖示。下方顯示雙方票數與總票數,並提供兩個投票按鈕。

動畫邏輯

每次投票後呼叫 playZoneMenu():先讓白色遮罩收合,接著淡入 VS,更新票數文字,再依比例算出新的 left 值,讓 VS 與紅色色帶同步移動。

var newLeft = hovertreecom / (hovertreecom + keleyicom) * 500 - 20 + "px"; // 20 為 vs 寬度的一半
$("#vs").animate({ left: newLeft }, 1000);
$("#red").animate({ width: newLeft }, 1000);

減去 20 像素是為了讓 VS 圖示的中心對齊分割線;紅色色帶的寬度則直接採用同一個數值,兩者視覺上才會吻合。

完整程式碼

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>jQuery仿搜狐投票效果演示 - 何問起</title><base target="_blank" />
 <link href="https://hovertree.com/texiao/jquery/21/css/hovertree.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="box_bg">
 <div id="container">
 <div id="green" class="line"></div>
 <div id="red" class="line"></div>
 <div id="white" class="line"></div>
 <div id="vs"></div>
 </div>
</div>
<div style="width:440px; margin:0px auto;">
 <div class="scope">正方<span id="hovertree"></span>票</div>
 <div class="scope">反方<span id="keleyi"></span>票</div>
 <div class="scope">總共<span id="myslider"></span>票</div>
</div>
<div style="width:440px; margin:0px auto;">
 <input value="投正方" id="tohovertree" type="button" />
 <input value="投反方" id="tokeleyi" type="button" style="float:right" />
 <div class="clearHovertree"></div>
</div>
 <script src="https://hovertree.com/ziyuan/jquery/jquery-1.11.3.min.js" type="text/javascript"></script>
 <script type="text/javascript">
 $(document).ready(function () {
 playZoneMenu();
 $("#tohovertree").on("click", function () { hovertreecom++; playZoneMenu(); })
 $("#tokeleyi").on("click", function () { keleyicom++; playZoneMenu(); })
 });
var hovertreecom = 20;
var keleyicom = 6;
function playZoneMenu()
{
 $("#white").animate({ width: 0, left:"250px"}, 1000 ,function(){
 $("#vs").fadeIn("slow",function(){
 $("#myslider").html(hovertreecom + keleyicom); $("#hovertree").html(hovertreecom); $("#keleyi").html(keleyicom);
 var newLeft = hovertreecom / (hovertreecom + keleyicom) * 500 - 20 + "px";
 $("#vs").animate({left:newLeft}, 1000);
 $("#red").animate({width:newLeft}, 1000);
 });
 });
 }
 </script>
</body>
</html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved