javascript - 为什么导航颜色不变

标签 javascript jquery html

您好,我正在编写此代码以根据 url 地址更改我的导航链接颜色,但它无法正常运行,请帮助我。我想更改该特定网址的导航链接的颜色 我的js代码

function big(x){

x.style.fontSize = "17px";
x.style.color="#03c1cb";
}
function small(x){
var y=location.hash;
if(x.href== y){
x.style.fontSize = "17px";
x.style.color="#03c1cb";
}
else{
x.style.color = "white";
x.style.fontSize ="15px";
}
}

function isElementInViewport (el) {
      //special bonus for those using jQuery
      if (typeof jQuery === "function" && el instanceof jQuery) {
        el = el[0];
      }
      var rect = el.getBoundingClientRect();
      return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $j(window).height() */
        rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $j(window).width() */
      );
    }

// url change on clicking
$(document).ready(function () {
    $(".scroll").click(function (e) {
        e.preventDefault();
        var section = this.href,
            sectionClean = section.substring(section.indexOf("#"));

        $("html, body").animate({
            scrollTop: $j(sectionClean).offset().top
        }, 1000, function () {
            window.location.hash = sectionClean;
        });

    });
});
// listen for the scroll event
    $(document).on("scroll", function() {
      console.log("onscroll event fired...");
      // check if the anchor elements are visible
      $(".anchor").each(function (idx, el) {
        if ( isElementInViewport(el) ) {
          // update the URL hash
          if (window.history.pushState) {
            var urlHash = "#" + $j(el).attr("id");
            window.history.pushState(null, null, urlHash);
          }
        }
      });
    });

我的html代码是

<a href="#home" id="start1" class="scroll" style="text-decoration:none;position:absolute;right:450px;top:37px;font-weight:bold;color:white;font-size:15px;z-index:200;transition:0.5s"  onmouseover="big(this)" onmouseout="small(this)">HOME</a>

请帮帮我

最佳答案

您将 href 颜色设置为白色,因此它不可见。将背景颜色更改为深色

<!DOCTYPE html>

<head>
  <meta charset="utf-8">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

  <style>
     body {background-color:lightgray}
  </style>
</head>
<body>
   <a href="#home" id="start1" class="scroll" style="text-decoration:none;position:absolute;right:450px;top:37px;font-weight:bold;color:white;font-size:15px;z-index:200;transition:0.5s"  onmouseover="big(this)" onmouseout="small(this)">HOME</a>
<script>
   function big(x){
     console.log(x.id);
     var x = document.getElementById(x.id);
   x.style.fontSize = "17px";
   x.style.color="#03c1cb";
   }
   function small(x){
   var y=location.hash;
   if(x.href== y){
   x.style.fontSize = "17px";
   x.style.color="#03c1cb";
   }
   else{
   x.style.color = "white";
   x.style.fontSize ="15px";
   }
 }
   </script>
</body>
</html>

关于javascript - 为什么导航颜色不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30763072/

相关文章:

javascript - 在 Google map 中动态更改信息窗口的内容

javascript - UTF-8 编码算法如何在 8 位 block 上工作(在 JavaScript 中)?

php - 在不使用 FormData() 的情况下使用 ajax 将图像发送到 php 文件

javascript - 使用 JQuery 在 Word Web 插件中选择文本

c# - Bootstrap 模态显示在页面上,而不是隐藏

jquery - CSS3 动画不会在 removeClass 和 addClass 上再次播放,但会在 toggleClass 上改变

javascript - 计算器 - 第一个计算结果显示,但第二个计算结果不显示

javascript - jQuery中通过data属性和部分id获取元素

javascript - 每 100 毫秒重新绘制一次正方形,但移动不流畅

javascript - React Router,单击浏览器后退按钮时的生命周期