浏览代码

移除无用依赖并优化脚本加载与代码格式

移除了对 `bootstrapNews` 插件的依赖,优化了 `MiddlewareExtension.cs` 和 `_Layout.cshtml` 中的脚本加载逻辑,删除了冗余代码。
对 `scripts.js` 文件进行了代码格式化,保留并优化了导航栏滚动事件、按钮点击效果、公告层逻辑、延迟检测及图片加载失败重试功能。
统一代码缩进,提升可读性和维护性。
懒得勤快 1 月之前
父节点
当前提交
2569db207f

+ 0 - 1
src/Masuit.MyBlogs.Core/Extensions/MiddlewareExtension.cs

@@ -99,7 +99,6 @@ public static class MiddlewareExtension
                 .Include("/Scripts/ripplet.js")
                 .Include("/Scripts/global/functions.js")
                 .Include("/Scripts/global/scripts.js")
-                .Include("/Assets/newsbox/jquery.bootstrap.newsbox.js")
                 .Include("/Assets/tagcloud/js/tagcloud.js")
                 .Include("/Assets/scrolltop/js/scrolltop.js")
                 .Include("/Assets/nav/js/main.js");

+ 2 - 4
src/Masuit.MyBlogs.Core/Views/Shared/_Layout.cshtml

@@ -65,12 +65,11 @@
   <environment names="Stage,Production">
     @await Styles.RenderAsync("~/bundles/main.css")
   </environment>
-  
   <environment names="Development">
-  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.js"></script>
+    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.js"></script>
   </environment>
   <environment names="Stage,Production">
-  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.prod.js"></script>
+    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.prod.js"></script>
   </environment>
   <script src="https://cdn.jsdelivr.net/npm/naive-ui"></script>
   <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
@@ -84,7 +83,6 @@
     <script src="~/Scripts/ripplet.js"></script>
     <script src="~/Scripts/global/functions.js"></script>
     <script src="~/Scripts/global/scripts.js"></script>
-    <script src="~/Assets/newsbox/jquery.bootstrap.newsbox.js"></script>
     <script src="~/Assets/tagcloud/js/tagcloud.js"></script>
     <script src="~/Assets/scrolltop/js/scrolltop.js"></script>
     <script src="~/Assets/nav/js/main.js"></script>

+ 185 - 197
src/Masuit.MyBlogs.Core/wwwroot/Scripts/global/scripts.js

@@ -1,227 +1,215 @@
 ; (function ($) {
-  $.fn.serializeObject = function () {
-    var o = {};
-    var a = this.serializeArray();
-    $.each(a, function () {
-      if (o[this.name]) {
-        if (!o[this.name].push) {
-          o[this.name] = [o[this.name]];
-        }
-        o[this.name].push(this.value || '');
-      } else {
-        o[this.name] = this.value || '';
-      }
-    });
-    return o;
-  }
+    $.fn.serializeObject = function () {
+        var o = {};
+        var a = this.serializeArray();
+        $.each(a, function () {
+            if (o[this.name]) {
+                if (!o[this.name].push) {
+                    o[this.name] = [o[this.name]];
+                }
+                o[this.name].push(this.value || '');
+            } else {
+                o[this.name] = this.value || '';
+            }
+        });
+        return o;
+    }
 })(jQuery);
 
 $(function () {
-  $(".notices").bootstrapNews({
-    newsPerPage: 4,
-    autoplay: true,
-    pauseOnHover: true,
-    navigation: false,
-    direction: 'down',
-    newsTickerInterval: 2500,
-    onToDo: function () {
-      //console.log(this);
-    }
-  });
-
-  var nav = $(".cd-main-header");
-  if (document.documentElement.scrollTop || document.body.scrollTop > 0) {
-    nav.css("background-color", "white");
-  } else {
-    nav.css("background-color", "transparent");
-  }
-  document.onscroll = function () {
-    if (document.documentElement.scrollTop || document.body.scrollTop > 10) {
-      nav.css({
-        "background-color": "white",
-        transition: "all 1s ease-in-out"
-      });
+    var nav = $(".cd-main-header");
+    if (document.documentElement.scrollTop || document.body.scrollTop > 0) {
+        nav.css("background-color", "white");
     } else {
-      nav.css({
-        "background-color": "transparent",
-        transition: "all 1s ease-in-out"
-      });
+        nav.css("background-color", "transparent");
+    }
+    document.onscroll = function () {
+        if (document.documentElement.scrollTop || document.body.scrollTop > 10) {
+            nav.css({
+                "background-color": "white",
+                transition: "all 1s ease-in-out"
+            });
+        } else {
+            nav.css({
+                "background-color": "transparent",
+                transition: "all 1s ease-in-out"
+            });
+        }
     }
-  }
 
-  $(".btn").on("mousedown", function (e) {
-    window.ripplet(e, {
-      color: null,
-      className: 'rainbow',
-      clearingDuration: '3s',
-      spreadingDuration: '1s'
+    $(".btn").on("mousedown", function (e) {
+        window.ripplet(e, {
+            color: null,
+            className: 'rainbow',
+            clearingDuration: '3s',
+            spreadingDuration: '1s'
+        });
     });
-  });
-  if (!Object.prototype.hasOwnProperty.call(window, 'event')) {
-    ['mousedown', 'mouseenter', 'onmouseleave'].forEach(function (eventType) {
-      window.addEventListener(eventType, function (event) {
-        window.event = event;
-      }, true);
-    });
-  }
-
-  window.fetch("/notice/last").then(function (response) {
-    return response.json();
-  }).then(function (data) {
-    if (!data.Success) {
-      return;
+    if (!Object.prototype.hasOwnProperty.call(window, 'event')) {
+        ['mousedown', 'mouseenter', 'onmouseleave'].forEach(function (eventType) {
+            window.addEventListener(eventType, function (event) {
+                window.event = event;
+            }, true);
+        });
     }
 
-    data = data.Data;
-    var nid = [].concat(JSON.parse(window.localStorage.getItem("notice") || '[]'));
-    if (data.StrongAlert && nid.indexOf(data.Id) == -1) {
-      //公告层
-      layer.open({
-        title: '网站公告:' + data.Title,
-        offset: (window.screen.width > 400 ? "100px" : "40px"),
-        area: (window.screen.width > 400 ? 400 : window.screen.width - 10) + 'px',
-        shade: 0.6,
-        closeBtn: true,
-        content: data.Content,
-        btn: ["查看详情", '知道了'],
-        btn1: function (layero) {
-          nid.push(data.Id);
-          window.localStorage.setItem("notice", JSON.stringify(nid));
-          window.location.href = "/notice/" + data.Id;
-        },
-        btn2: function (index) {
-          nid.push(data.Id);
-          window.localStorage.setItem("notice", JSON.stringify(nid));
-          layer.closeAll();
+    window.fetch("/notice/last").then(function (response) {
+        return response.json();
+    }).then(function (data) {
+        if (!data.Success) {
+            return;
         }
-      });
-    }
-  }).catch(function (e) {
-    console.log("Oops, error");
-  });
 
-  setInterval(function () {
-    let timestamp = new Date().getTime();
-    DotNet.invokeMethodAsync('Masuit.MyBlogs.Core', 'Latency').then(data => {
-      $("#ping").text(new Date().getTime() - timestamp);
+        data = data.Data;
+        var nid = [].concat(JSON.parse(window.localStorage.getItem("notice") || '[]'));
+        if (data.StrongAlert && nid.indexOf(data.Id) == -1) {
+            //公告层
+            layer.open({
+                title: '网站公告:' + data.Title,
+                offset: (window.screen.width > 400 ? "100px" : "40px"),
+                area: (window.screen.width > 400 ? 400 : window.screen.width - 10) + 'px',
+                shade: 0.6,
+                closeBtn: true,
+                content: data.Content,
+                btn: ["查看详情", '知道了'],
+                btn1: function (layero) {
+                    nid.push(data.Id);
+                    window.localStorage.setItem("notice", JSON.stringify(nid));
+                    window.location.href = "/notice/" + data.Id;
+                },
+                btn2: function (index) {
+                    nid.push(data.Id);
+                    window.localStorage.setItem("notice", JSON.stringify(nid));
+                    layer.closeAll();
+                }
+            });
+        }
+    }).catch(function (e) {
+        console.log("Oops, error");
     });
-  }, 2000);
 
-  // 自动重试加载图片
-  $('img').on("error", function () {
-    var that = $(this);
-    var retry = that.attr("retry") || 0;
-    if (retry > 10) {
-      return;
-    } else {
-      retry++;
-      that.attr("retry", retry); //重试次数+1
-      that.attr('src', that.attr("src")); //继续刷新图片
-    }
-  });
-  $('img').on("abort", function () {
-    var that = $(this);
-    var retry = that.attr("retry") || 0;
-    if (retry > 10) {
-      return;
-    } else {
-      retry++;
-      that.attr("retry", retry); //重试次数+1
-      that.attr('src', that.attr("src")); //继续刷新图片
-    }
-  });
+    setInterval(function () {
+        let timestamp = new Date().getTime();
+        DotNet.invokeMethodAsync('Masuit.MyBlogs.Core', 'Latency').then(data => {
+            $("#ping").text(new Date().getTime() - timestamp);
+        });
+    }, 2000);
+
+    // 自动重试加载图片
+    $('img').on("error", function () {
+        var that = $(this);
+        var retry = that.attr("retry") || 0;
+        if (retry > 10) {
+            return;
+        } else {
+            retry++;
+            that.attr("retry", retry); //重试次数+1
+            that.attr('src', that.attr("src")); //继续刷新图片
+        }
+    });
+    $('img').on("abort", function () {
+        var that = $(this);
+        var retry = that.attr("retry") || 0;
+        if (retry > 10) {
+            return;
+        } else {
+            retry++;
+            that.attr("retry", retry); //重试次数+1
+            that.attr('src', that.attr("src")); //继续刷新图片
+        }
+    });
 });
 
 /**
  * 鼠标桃心
  */
 (function (window, document) {
-  var hearts = [];
-  window.requestAnimationFrame = (function () {
-    return window.requestAnimationFrame ||
-      window.webkitRequestAnimationFrame ||
-      window.mozRequestAnimationFrame ||
-      window.oRequestAnimationFrame ||
-      window.msRequestAnimationFrame ||
-      function (callback) {
-        setTimeout(callback, 1000 / 60);
-      }
-  })();
-  init();
+    var hearts = [];
+    window.requestAnimationFrame = (function () {
+        return window.requestAnimationFrame ||
+            window.webkitRequestAnimationFrame ||
+            window.mozRequestAnimationFrame ||
+            window.oRequestAnimationFrame ||
+            window.msRequestAnimationFrame ||
+            function (callback) {
+                setTimeout(callback, 1000 / 60);
+            }
+    })();
+    init();
 
-  function init() {
-    css(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
-    attachEvent();
-    gameloop();
-  }
+    function init() {
+        css(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
+        attachEvent();
+        gameloop();
+    }
 
-  function gameloop() {
-    for (var i = 0; i < hearts.length; i++) {
-      if (hearts[i].alpha <= 0) {
-        document.body.removeChild(hearts[i].el);
-        hearts.splice(i, 1);
-        continue;
-      }
-      hearts[i].y--;
-      hearts[i].scale += 0.004;
-      hearts[i].alpha -= 0.013;
-      hearts[i].el.style.cssText =
-        "left:" +
-        hearts[i].x +
-        "px;top:" +
-        hearts[i].y +
-        "px;opacity:" +
-        hearts[i].alpha +
-        ";transform:scale(" +
-        hearts[i].scale +
-        "," +
-        hearts[i].scale +
-        ") rotate(45deg);background:" +
-        hearts[i].color;
+    function gameloop() {
+        for (var i = 0; i < hearts.length; i++) {
+            if (hearts[i].alpha <= 0) {
+                document.body.removeChild(hearts[i].el);
+                hearts.splice(i, 1);
+                continue;
+            }
+            hearts[i].y--;
+            hearts[i].scale += 0.004;
+            hearts[i].alpha -= 0.013;
+            hearts[i].el.style.cssText =
+                "left:" +
+                hearts[i].x +
+                "px;top:" +
+                hearts[i].y +
+                "px;opacity:" +
+                hearts[i].alpha +
+                ";transform:scale(" +
+                hearts[i].scale +
+                "," +
+                hearts[i].scale +
+                ") rotate(45deg);background:" +
+                hearts[i].color;
+        }
+        requestAnimationFrame(gameloop);
     }
-    requestAnimationFrame(gameloop);
-  }
 
-  function attachEvent() {
-    var old = typeof window.onclick === "function" && window.onclick;
-    window.onclick = function (event) {
-      old && old();
-      createHeart(event);
+    function attachEvent() {
+        var old = typeof window.onclick === "function" && window.onclick;
+        window.onclick = function (event) {
+            old && old();
+            createHeart(event);
+        }
     }
-  }
 
-  function createHeart(event) {
-    var d = document.createElement("div");
-    d.className = "heart";
-    hearts.push({
-      el: d,
-      x: event.clientX - 5,
-      y: event.clientY - 5,
-      scale: 1,
-      alpha: 1,
-      color: randomColor()
-    });
-    document.body.appendChild(d);
-  }
+    function createHeart(event) {
+        var d = document.createElement("div");
+        d.className = "heart";
+        hearts.push({
+            el: d,
+            x: event.clientX - 5,
+            y: event.clientY - 5,
+            scale: 1,
+            alpha: 1,
+            color: randomColor()
+        });
+        document.body.appendChild(d);
+    }
 
-  function css(css) {
-    var style = document.createElement("style");
-    style.type = "text/css";
-    try {
-      style.appendChild(document.createTextNode(css));
-    } catch (ex) {
-      style.styleSheet.cssText = css;
+    function css(css) {
+        var style = document.createElement("style");
+        style.type = "text/css";
+        try {
+            style.appendChild(document.createTextNode(css));
+        } catch (ex) {
+            style.styleSheet.cssText = css;
+        }
+        document.getElementsByTagName('head')[0].appendChild(style);
     }
-    document.getElementsByTagName('head')[0].appendChild(style);
-  }
 
-  function randomColor() {
-    return "rgb(" +
-      (~~(Math.random() * 255)) +
-      "," +
-      (~~(Math.random() * 255)) +
-      "," +
-      (~~(Math.random() * 255)) +
-      ")";
-  }
+    function randomColor() {
+        return "rgb(" +
+            (~~(Math.random() * 255)) +
+            "," +
+            (~~(Math.random() * 255)) +
+            "," +
+            (~~(Math.random() * 255)) +
+            ")";
+    }
 })(window, document);