2016年8月8日 星期一

[Chrome] Notification 通知視窗




  1. window.addEventListener('load', function () {
  2. //確認使用者是否允許跳窗,如果沒有,就跳窗取權限
  3. if (window.Notification && Notification.permission !== "granted") {
  4. Notification.requestPermission(function (status) {
  5. if (Notification.permission !== status) {
  6. Notification.permission = status;
  7. }
  8. });
  9. }
  10. function NotifyMsg() {
  11. var option = {
  12. tag: 'Notification',
  13. body: '測試測試測試',
  14. data: 'I am a data',
  15. icon: '' //可以自訂ICON
  16. }
  17.  
  18. var n = new Notification("Title", option);
  19. setTimeout(n.close.bind(n), 5000);
  20. console.log(n.data);
  21. n.onclick = function (event) {
  22. event.preventDefault(); // prevent the browser from focusing the Notification's tab
  23. window.open('http://www.google.com.tw/', '_blank');
  24. }
  25. }
  26.  
  27. var button = document.getElementsByTagName('button')[0];
  28. button.addEventListener('click', function () {
  29. // If the user agreed to get notified
  30. // Let's try to send ten notifications
  31. if (window.Notification && Notification.permission === "granted") {
  32. NotifyMsg();
  33. }
  34.  
  35. // If the user hasn't told if he wants to be notified or not
  36. // Note: because of Chrome, we are not sure the permission property
  37. // is set, therefore it's unsafe to check for the "default" value.
  38. else if (window.Notification && Notification.permission !== "denied") {
  39. Notification.requestPermission(function (status) {
  40. if (status === "granted") {
  41. NotifyMsg();
  42. }
  43. // Otherwise, we can fallback to a regular modal alert
  44. else {
  45. alert("Hi!");
  46. }
  47. });
  48. }
  49. // If the user refuses to get notified
  50. else {
  51. // We can fallback to a regular modal alert
  52. alert("Hi!");
  53. }
  54. });
  55. });



參考資料: MDN


2016年8月3日 星期三

[JQuery] slideShow 輪播

記錄一下 好用的輪播套件 slick 連結

然後下面範例是拿其他教學範例多加的功能

在特定的高度上會停止撥放幻燈片

  1. $(document).ready(function (){
  2. //window.scroll 抓取使用者滾輪高度..這裡自訂600
  3. var stop = false;
  4. $(window).scroll(function () {
  5. var scrollTop = $(window).scrollTop();
  6. if (scrollTop >= 600 ) {
  7. console.log("stop" + scrollTop);
  8. stop = true;
  9. } else {
  10. console.log("r" + scrollTop);
  11. stop = false;
  12. }
  13. });
  14.  
  15. var num = 1;
  16. var tNum = 5;
  17. var duration = 2000;
  18. console.log("A");
  19.  
  20. run();
  21. $("#box").mouseover(function () { stopRun(); })
  22. .mouseout(function () { run();})
  23. for (var i = 1; i <= tNum; i++) {
  24. document.getElementById("tab" + i).onclick = show;
  25. document.getElementById("con" + i).style.display = "none";
  26. }
  27. document.getElementById("con1").style.display = "block";
  28. document.getElementById("tab1").className = "now-tab";
  29.  
  30. //在 autoShow 判斷是否停止撥放
  31. function autoShow() {
  32. if (stop) return;
  33.  
  34. for (var i = 1; i <= tNum; i++) {
  35. document.getElementById("con" + i).style.display = "none";
  36. document.getElementById("tab" + i).className = "";
  37. }
  38. if (num < tNum) { num++; } else { num = 1; }
  39. document.getElementById("con" + num).style.display = "block";
  40. document.getElementById("tab" + num).className = "now-tab";
  41.  
  42. }
  43.  
  44. function show() {
  45. num = this.id.substr(3) - 1;
  46. autoShow();
  47. }
  48.  
  49. function stopRun() { clearInterval(myInterval); }
  50.  
  51. function run() { myInterval = setInterval(autoShow, duration); }
  52. });
來源:Flycan-輪播廣告 教學