歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> CSS 3模仿Android 中的toast效果

CSS 3模仿Android 中的toast效果

日期:2017/3/1 10:37:10   编辑:Linux編程

在Android中,可以使用toast搞出一個信息提示的效果,在CSS 3中,其實也可以

模仿一下,如下代碼,先是CSS:

  1. #toast{
  2. position: fixed;
  3. top: 20px;
  4. left: 50%;
  5. width: 200px;
  6. margin-left: -100px;
  7. border: 1px solid #666;
  8. background-color: #B1BCCF;
  9. padding: 10px 0 ;
  10. text-align:center;
  11. opacity: .9;
  12. /*The good stuff */
  13. -webkit-transition: opacity 0.5s ease-out; /* Saf3.2+, Chrome */
  14. -moz-transition: opacity 0.5s ease-out; /* FF4+ */
  15. -ms-transition: opacity 0.5s ease-out; /* IE10? */
  16. -o-transition: opacity 0.5s ease-out; /* Opera 10.5+ */
  17. transition: opacity 0.5s ease-out;
  18. }

之後設計一個按鈕,然後設計JAVASCRIPT代碼如下:

  1. <script type="text/javascript">
  2. var intervalCounter = 0;
  3. function hideToast(){
  4. var alert = document.getElementById("toast");
  5. alert.style.opacity = 0;
  6. clearInterval(intervalCounter);
  7. }
  8. function drawToast(message){
  9. var alert = document.getElementById("toast");
  10. if (alert == null){
  11. var toastHTML = '<div id="toast">' + message + '</div>';
  12. document.body.insertAdjacentHTML('beforeEnd', toastHTML);
  13. }
  14. else{
  15. alert.style.opacity = .9;
  16. }
  17. intervalCounter = setInterval("hideToast()", 1000);
  18. }
  19. function save(){
  20. drawToast("Item saved");
  21. }
  22. </script>
  23. <button onclick="save()">Save</button>

可惜只能在非IE下運行了。

Copyright © Linux教程網 All Rights Reserved