简单的 jQuery 和 CSS 导航在刷新时随机无法正常工作...所有浏览器

发布于 2024-12-10 09:50:46 字数 494 浏览 0 评论 0原文

好吧,似乎所有浏览器和分辨率都是如此。我是编码新手,所以我相当确定这是我遗漏的一个简单错误。我问了一群朋友,看来大家的情况都是一样的。导航悬停仅在主编码布局上的某些时候起作用(尚未完全编码)。我自己把导航拉出来,它似乎 100% 都能正常工作。

您可以在此处查看它的工作原理:http://dperolio.com/newnav/css/

这里就是问题所在: http://dperolio.com/tealtop2

它最初可能对您有用,也可能不是。如果您垃圾邮件刷新(Ctrl+R),有时会起作用。当它不起作用时,无论如何它似乎都处于悬停状态(粗体白色链接)。

我很感激你能提供的任何见解;我再次确信我刚刚犯了一些愚蠢的错误,希望更有经验的人能够发现它并为我指出?谢谢!

Okay, this seems to be the case in all browsers and resolutions. I'm brand new to coding, so I'm fairly sure it's a simple error I'm missing. I've asked a group of friends, and it seems to be the same for everyone. The navigation hover only works some of the time on the main coded layout (not fully coded yet). I pulled the navigation out by itself and it seems to work correctly 100% of the time.

You can view how it should work here: http://dperolio.com/newnav/css/

Here is where the problem is: http://dperolio.com/tealtop2

It may work for you initially, or it may not. If you spam refresh (Ctrl+R) it will sometimes work. When it doesn't work, it seems it just sits in the hover state no matter what (bold white links).

I appreciate any insight you can offer; again I'm fairly sure I just made some stupid mistake and hopefully someone more experienced can spot it and point it out for me? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难理解 2024-12-17 09:50:46

我知道这是愚蠢的...这是因为我在调用 jQuery 之后而不是之前列出了外部 CSS。

I knew it was something stupid... It was because I had the external CSS listed after calling the jQuery instead of before it.

夜无邪 2024-12-17 09:50:46

这是我为您制作的一个快速演示:

$(document).ready(function() {

  $("#topnav li").prepend("<span></span>"); //Throws an empty span tag right before the a tag

  $("#topnav li").each(function() { //For each list item...
    var linkText = $(this).find("a").html(); //Find the text inside of the a tag
    $(this).find("span").show().html(linkText); //Add the text in the span tag
  });

  $("#topnav li").hover(function() { //On hover...
    $(this).find("span").stop().animate({
      marginTop: "-40" //Find the span tag and move it up 40 pixels
    }, 250);
  }, function() { //On hover out...
    $(this).find("span").stop().animate({
      marginTop: "0" //Move the span back to its original state (0px)
    }, 250);
  });

});
ul#topnav {
  margin: 0;
  padding: 0;
  list-style: none;
  float: left;
  font-size: 1.1em;
}

ul#topnav li {
  margin: 0;
  padding: 0;
  overflow: hidden;
  /*--Important - Masking out the hover state by default--*/
  float: left;
  height: 40px;
}

ul#topnav a,
ul#topnav span {
  /*--The <a> and <span> share the same properties since the <span>  will be a duplicate of the <a> tag--*/
  padding: 10px 20px;
  float: left;
  text-decoration: none;
  color: #000;
  background: url(a_bg.gif) repeat-x;
  text-transform: uppercase;
  clear: both;
  width: 100%;
  height: 20px;
  line-height: 20px;
  /*--Vertical alignment of text--*/
}

ul#topnav a {
  /*--This is basically the hover state of navigation--*/
  color: #555;
  background-position: left bottom;
}

ul#topnav span {
  /*--Default state of navigation--*/
  background-position: left top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <ul id="topnav" class="v2">
    <li><a href="http://www.sohtanaka.com/">Home</a></li>
    <li><a href="http://www.sohtanaka.com/web-design/web-design-services.php">Services</a></li>
    <li><a href="http://www.sohtanaka.com/web-design/designbombscom/">Portfolio</a></li>
    <li><a href="http://www.sohtanaka.com/web-design-blog/">Blog</a></li>
    <li><a href="http://www.sohtanaka.com/about/">About</a></li>
  </ul>
</div>

Here is a quick demo I made for you:

$(document).ready(function() {

  $("#topnav li").prepend("<span></span>"); //Throws an empty span tag right before the a tag

  $("#topnav li").each(function() { //For each list item...
    var linkText = $(this).find("a").html(); //Find the text inside of the a tag
    $(this).find("span").show().html(linkText); //Add the text in the span tag
  });

  $("#topnav li").hover(function() { //On hover...
    $(this).find("span").stop().animate({
      marginTop: "-40" //Find the span tag and move it up 40 pixels
    }, 250);
  }, function() { //On hover out...
    $(this).find("span").stop().animate({
      marginTop: "0" //Move the span back to its original state (0px)
    }, 250);
  });

});
ul#topnav {
  margin: 0;
  padding: 0;
  list-style: none;
  float: left;
  font-size: 1.1em;
}

ul#topnav li {
  margin: 0;
  padding: 0;
  overflow: hidden;
  /*--Important - Masking out the hover state by default--*/
  float: left;
  height: 40px;
}

ul#topnav a,
ul#topnav span {
  /*--The <a> and <span> share the same properties since the <span>  will be a duplicate of the <a> tag--*/
  padding: 10px 20px;
  float: left;
  text-decoration: none;
  color: #000;
  background: url(a_bg.gif) repeat-x;
  text-transform: uppercase;
  clear: both;
  width: 100%;
  height: 20px;
  line-height: 20px;
  /*--Vertical alignment of text--*/
}

ul#topnav a {
  /*--This is basically the hover state of navigation--*/
  color: #555;
  background-position: left bottom;
}

ul#topnav span {
  /*--Default state of navigation--*/
  background-position: left top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <ul id="topnav" class="v2">
    <li><a href="http://www.sohtanaka.com/">Home</a></li>
    <li><a href="http://www.sohtanaka.com/web-design/web-design-services.php">Services</a></li>
    <li><a href="http://www.sohtanaka.com/web-design/designbombscom/">Portfolio</a></li>
    <li><a href="http://www.sohtanaka.com/web-design-blog/">Blog</a></li>
    <li><a href="http://www.sohtanaka.com/about/">About</a></li>
  </ul>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文