移動端點選彈出提示視窗程式碼例項

admin發表於2017-02-23

分享一段程式碼例項,它實現了移動端點選彈出提示視窗的效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"/>
<title>螞蟻部落</title>
<style type="text/css">
* {
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-overflow-scrolling: touch;
  font-family: "Helvetica Neue",Helvetica,"songti",STHeiTi,sans-serif;
  font-size: 16px;
}
.main {
  width: 332px;
  height: auto;
  margin: 100px auto;
}
.input {
  width: 300px;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  -webkit-appearance: none;
}
.button {
  outline: none;
  -webkit-appearance: none;
  border: none;
  color: #fff;
  font-size: 18px;
  width: 300px;
  height: 46px;
  background: #189cfb;
  border-radius: 4px;
  margin-top: 30px;
  -webkit-transition: 0.3s;
  cursor: pointer;
}
.button:active {
  background: #1170b4;
}
/*不想多寫ie等瀏覽器相容的分割線*/
.alert {
  min-width: 10px;
  max-width: 280px;
  position: fixed;
  bottom: 80px;
  background: rgba(0,0,0,0.6);
  border-radius: 6px;
  color: #eee;
  font-size: 16px;
  padding: 10px 15px;
  text-align: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
  left: 50%;
  -webkit-transform: translate3d(-50%,0,0);
  -webkit-animation: alert 4s;
}
@-webkit-keyframes alert {
  0% {
    -webkit-transform: translate3d(-50%,0,0);
    opacity: 0;
  }
  10% {
    -webkit-transform: translate3d(-50%,-30px,0);
  }
  15% {
    -webkit-transform: translate3d(-50%,-25px,0);
    opacity: 1;
  }
  100% {
    -webkit-transform: translate3d(-50%,-25px,0);
  }
}
</style>
</head>
<body>
<div class="main">
  <input type="search" class="input" value="螞蟻部落">
  <button class="button" id="btn">檢視效果</button>
  </div>
<script type="text/javascript">
var btn = document.querySelector('#btn');
btn.onclick = function() {
  var t = document.querySelector('input').value;
  msg.alert(t)
}
 
var msg = {
  alert: function(t) {
    var alt = document.querySelector('.alert');
    if (alt) {
      alt.innerHTML = t;
      return;
    };
    var h = document.createElement('div');
    h.innerHTML = t || '為什麼不說話 (╰_╯)';
    h.setAttribute('class', 'alert');
    document.querySelector('body').appendChild(h);
    msg.fire(h);
  },
  fire: function(obj) {
    var evt = ['webkitAnimationEnd', 'mozAnimationEnd', 'msAnimationEnd', 'oAnimationEnd', 'animationEnd'];
    for (var index = 0; index < evt.length; index++) {
      obj.addEventListener(evt[index], function() {
        this.parentNode.removeChild(this);
      });
    }
  }
};
</script>
</body>
</html>

相關文章