如何制作滚动菜单?

时间:2015-06-30 15:11:48

标签: jquery html css menu

我想制作滚动菜单,我在codpen.io中使用了这个样本鳕鱼:

ayGvD

,但我做不到。

我的菜单在那里:

BNJXmV

请帮帮我

我很抱歉因为我的英语不好!

1 个答案:

答案 0 :(得分:0)

您还没有导入jQuery库。在在线测试环境中,如JSFiddle和CodePen,添加<script src="sth.js"></script>将无法正常工作,因为他们已将HTML,JS和CSS分离为单独的“控制台”。您必须使用菜单导入外部库。

https://blog.codepen.io/documentation/editor/adding-external-resources/

您已修改的CodePen

// Create a clone of the menu, right next to original.
$('.menu').addClass('original').clone().insertAfter('.menu').addClass('cloned').css('position','fixed').css('top','0').css('margin-top','0').css('z-index','500').removeClass('original').hide();

scrollIntervalID = setInterval(stickIt, 10);


function stickIt() {

  var orgElementPos = $('.original').offset();
  orgElementTop = orgElementPos.top;               

  if ($(window).scrollTop() >= (orgElementTop)) {
// scrolled past the original position; now only show the cloned, sticky element.

// Cloned element should always have same left position and width as original element.     
orgElement = $('.original');
coordsOrgElement = orgElement.offset();
leftOrgElement = coordsOrgElement.left;  
widthOrgElement = orgElement.css('width');
$('.cloned').css('left',leftOrgElement+'px').css('top',0).css('width',widthOrgElement).show();
$('.original').css('visibility','hidden');
  } else {
// not scrolled past the menu; only show the original menu.
$('.cloned').hide();
$('.original').css('visibility','visible');
  }
}
*{
   margin: 0;
   padding: 0;
}
.menu {
  width: 100%;
  float: left;
  margin-left: 0px;
  text-align: left;
  background-color: #2f4a67;
  margin-bottom: 0px;
  height: 50px;
}
.menu ul {
  margin-top: 0px;
}
.menu ul a li {
  display: inline-block;
  margin-right: 4%;
  padding-right: 1%;
  padding-top: 0.6%;
  padding-bottom: 0.6%;
  padding-left: 1%;
  color: #FFFFFF;
  margin-top: 5px;
  font-weight: bold;
  font-size: 1.em;
  font-size: 1.3em;
}
.menu li:hover {
  background-color: #FFFFFF;
  color: #2F4A67;
}
h3 {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<div class="menu">

  <ul>

    <a href="#">
      <li>Home</li>
    </a>
    <a href="#">
      <li>Abot me</li>
    </a>



  </ul>

</div>
</header>
<div class="content">
<h3 style="display: block;">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
</h3>
</div>