DIV CSS 佈局教程網

用 jQuery 打造精美的彈出提示層特效
編輯:JavaScript綜合知識     

下面這個效果由 jQuery 負責控制邏輯、CSS 負責動畫與外觀:點擊按鈕後,半透明遮罩淡入,一張白色提示卡從上方滑下,中央的綠色勾選圓標先是放大後縮回正常尺寸,最後再淡出收場。

HTML 結構

<div class='b'></div>
<div class='bb'></div>
<button id='go'>GO</button>

<div class='message'>
  <div class='check'>✔</div>
  <p>Success</p>
  <p>Check your email for a booking confirmation. We'll see you soon!</p>
  <button id='ok'>OK</button>
</div>

<script src='jquery.js'></script>
<script>
$('#go').click(function () { go(50); });
$('#ok').click(function () { go(500); });

setTimeout(function () { go(50); }, 700);
setTimeout(function () { go(500); }, 2000);

function go(nr) {
  $('.bb').fadeToggle(200);
  $('.message').toggleClass('comein');
  $('.check').toggleClass('scaledown');
  $('#go').fadeToggle(nr);
}
//@ sourceURL=pen.js
</script>

CSS 樣式

body, html {
  height: 100%;
  font-size: 20px;
  font-family: Source Sans Pro;
}
.b, .bb {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("kje4L5j.jpg");
  background-attachment: fixed;
  background-size: cover;
  background-position: center;
}
.bb {
  background: url("bDBs0et.jpg");
  background-attachment: fixed;
  background-size: cover;
  background-position: center;
  display: none;
}
#go {
  position: absolute;
  top: 30px;
  left: 50%;
  transform: translate(-50%, 0%);
  color: white;
  border: 0;
  background: #71c341;
  width: 100px;
  height: 30px;
  border-radius: 6px;
  font-size: 1rem;
  transition: background 0.2s ease;
  outline: none;
}
#go:hover  { background: #8ecf68; }
#go:active { background: #5a9f32; }

.message {
  position: absolute;
  top: -200px;
  left: 50%;
  transform: translate(-50%, 0%);
  width: 300px;
  background: white;
  border-radius: 8px;
  padding: 30px;
  text-align: center;
  font-weight: 300;
  color: #2c2928;
  opacity: 0;
  transition: top 0.3s cubic-bezier(0.31, 0.25, 0.5, 1.5),
              opacity 0.2s ease-in-out;
}
.message .check {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, -50%) scale(4);
  width: 120px;
  height: 110px;
  background: #71c341;
  color: white;
  font-size: 3.8rem;
  padding-top: 10px;
  border-radius: 50%;
  opacity: 0;
  transition: transform 0.2s 0.25s cubic-bezier(0.31, 0.25, 0.5, 1.5),
              opacity 0.1s 0.25s ease-in-out;
}
.message .scaledown {
  transform: translate(-50%, -50%) scale(1);
  opacity: 1;
}
.message p {
  font-size: 1.1rem;
  margin: 25px 0px;
  padding: 0;
}
.message p:nth-child(2) {
  font-size: 2.3rem;
  margin: 40px 0px 0px 0px;
}
.message #ok {
  position: relative;
  color: white;
  border: 0;
  background: #71c341;
  width: 100%;
  height: 50px;
  border-radius: 6px;
  font-size: 1.2rem;
  transition: background 0.2s ease;
  outline: none;
}
.message #ok:hover  { background: #8ecf68; }
.message #ok:active { background: #5a9f32; }

.comein {
  top: 150px;
  opacity: 1;
}

動畫是怎麼串起來的

全部魔法都在 go() 這個函式裡,它只做四件事:切換遮罩的淡入淡出、替提示卡加上 comein 類別讓它滑下、替勾選圓標加上 scaledown 讓它縮回原尺寸、最後淡入淡出 GO 按鈕。因為用的是 toggleClass,同一個函式同時負責「開」與「關」,不必寫兩套邏輯。

動畫的細節藏在 CSS 的 transition 與 cubic-bezier 裡:提示卡使用帶有超過 1 的 control point 的 cubic-bezier(0.31, 0.25, 0.5, 1.5),這會產生輕微的回彈感;勾選圓標則透過 transition-delay: 0.25s 延後半秒啟動,讓它比卡片晚一點出現,層次感就是這樣做出來的。

以上就是這套彈出層提示特效的完整程式碼,可以依自己的配色需求調整 #71c341 這組綠色,直接放進專案使用。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved