我正在尝试创建一个网站,如果你点击一个< li>那么< li>将添加一个类。然后它将向上滑动当前可见屏幕并将其隐藏,然后显示对应于< li> (例如,如果< li>是'home',那么它将滑动当前现有的屏幕并隐藏它,然后它应该是'#homeSlide'屏幕)。这是我的代码。
$(document).ready(function(){
hideItems(); //this function basically hides all the screens / slides.. The words 'screen' and 'slides' are interchangeable for the time being.
$('#homeSlide').fadeIn(1000); //the default screen to be shown is the home screen
$('#homeSlide').addClass('current'); //to signify that this is the current visible screen
$('#home').addClass('clicked'); //#home is the <li>, so it adds the .clicked class to the <li>
$('#sidebar ul a li').click(function(){ //loops through all <li>'s in the sidebar if an <li> is clicked
var current_slide = $(this);
$('#sidebar ul .clicked').removeClass('clicked'); // when an <li> is clicked, remove .clicked class from any other <li>'s
current_slide.addClass('clicked'); // add .clicked class to the clicked <li> ($(this))
hidePrevSlide(function(){
alert('enter showing step');
current_slide.showCurrentSlide();
});
});
});
这是我的hidePrevSlide函数。
function hidePrevSlide(){
var test = $('.current').attr('id');
test = "#" + test; // surrounds the id with a # and the word 'Slide'. This is the id of the screen which should slideUp
$(test).slideUp( function () {
$(test).hide();
$(test).removeClass('current');
});
alert('finished hiding step. Should enter showing step now');
};
现在,当我运行代码时,它确实说'完成了隐藏步骤。现在应该进入显示步骤'但它没有说'输入显示步',所以它不会进入hidePrevSlide功能完成后应该执行的步骤。怎么样?
答案 0 :(得分:2)
hidePrevSlide
需要调用回调函数。
function hidePrevSlide(callback){
var test = $('.current');
test.slideUp( function () {
test.hide();
test.removeClass('current');
});
alert('finished hiding step. Should enter showing step now');
callback();
};
我也删除了使用$(test)
。如果您有一个元素,则无需按ID继续搜索。