我在滚动时遇到自动菜单项问题。因为我的标题有2个菜单,所以看起来像:菜单1< LOGO>菜单2;所以我一直在使用这个脚本:
css
是否有可能以某种方式在此脚本中使用2个菜单?我的课程是public class DemoActivity extends Activity {
MediaPlayer mp;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mp = MediaPlayer.create(DemoActivity.this,R.raw.mus);
btn = (Button) findViewById(R.id.btnOk);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (btn.getText().toString().equalsIgnoreCase("play")) {
mp.start();
btn.setText("Pause");
} else if (btn.getText().toString().equalsIgnoreCase("pause")) {
mp.pause();
btn.setText("Play");
}
}
});
@Override
public void onPause() {
super.onPause();
if (mp.isPlaying()) {
mp.start();
} else {
mp.release();
}
}
@Override
protected void onRestart() {
try {
if (mp.isPlaying() && mp!=null) {
mp.start();
} else {
mp.release();
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
super.onRestart();
}
@Override
protected void onDestroy() {
super.onDestroy();
mp.release();
}
和// Cache selectors
var topMenu = $(".main-nav1"),
topMenuHeight = topMenu.outerHeight()+15,
// All list items
menuItems = topMenu.find("a"),
// Anchors corresponding to menu items
scrollItems = menuItems.map(function(){
var item = $($(this).attr("href"));
if (item.length) { return item; }
});
// Bind to scroll
$(window).scroll(function(){
// Get container scroll position
var fromTop = $(this).scrollTop()+topMenuHeight;
// Get id of current scroll item
var cur = scrollItems.map(function(){
if ($(this).offset().top < fromTop)
return this;
});
// Get the id of the current element
cur = cur[cur.length-1];
var id = cur && cur.length ? cur[0].id : "";
// Set/remove active class
menuItems
.parent().removeClass("active")
.end().filter("[href=#"+id+"]").parent().addClass("active");
});
。我真的需要jQuery的帮助,因为我是这种语言的新手!
答案 0 :(得分:1)
查看代码,可能只需更改此基本行:
menuItems = topMenu.add($('.main-nav2')).find('a'),
在让它更具可读性之后我才能看到它:
var topMenu = $('.main-nav1'),
topMenuHeight = topMenu.outerHeight()+15,
menuItems = topMenu.add($('.main-nav2')).find('a'),
scrollItems = menuItems.map(function() {
var item = $($(this).attr('href'));
if (item.length) return item;
});
$(window).scroll(function() {
var fromTop = $(this).scrollTop()+topMenuHeight,
cur = scrollItems.map(function() {
if ($(this).offset().top < fromTop)
return this;
});
cur = cur[cur.length-1];
var id = cur && cur.length ? cur[0].id : '';
menuItems
.parent().removeClass('active').end()
.filter('[href=#'+id+']').parent().addClass('active');
});