2013-04-24 46 views
1

我與Stellar.js玩弄和所使用的演示這裏找到: http://webdesign.tutsplus.com/tutorials/complete-websites/create-a-parallax-scrolling-website-using-stellar-js/視差/ Steller.js和jQuery航點

我遇到了同樣的問題,該演示頁面有,這一點,當頁面第一次加載時,導航(幻燈片1)不活動。它只有在開始向下滾動時纔會變爲活動狀態,然後當您返回到幻燈片1時,它將保持活動狀態。

這是在FireFox和IE8中發現的問題(沒有IE9測試)。

我確定它是一個航點問題,而不是在頁面加載時激活。我該如何修復它,以便導航中的「幻燈片1」轉到它的活動類狀態?

這裏是從演示JS代碼:

jQuery(document).ready(function ($) { 


//initialise Stellar.js 
$(window).stellar(
    { 
    // Set scrolling to be in either one or both directions 
    horizontalScrolling: true, 
    verticalScrolling: true,  
    // Refreshes parallax content on window load and resize 
    responsive: true, 



    } 
    ); 

//Cache some variables 
var links = $('.navigation').find('li'); 
slide = $('.slide'); 
button = $('.button'); 
mywindow = $(window); 
htmlbody = $('html,body'); 



//Setup waypoints plugin 
slide.waypoint(function (event, direction) { 

    //cache the variable of the data-slide attribute associated with each slide 
    dataslide = $(this).attr('data-slide'); 


    //If the user scrolls up change the navigation link that has the same data-slide attribute as the slide to active and 
    //remove the active class from the previous navigation link 
    if (direction === 'down') { 
     $('.navigation li[data-slide="' + dataslide + '"]').addClass('active').prev().removeClass('active'); 
    } 
    // else If the user scrolls down change the navigation link that has the same data-slide attribute as the slide to active and 
    //remove the active class from the next navigation link 
    else { 
     $('.navigation li[data-slide="' + dataslide + '"]').addClass('active').next().removeClass('active'); 
    } 

}); 


//waypoints doesnt detect the first slide when user scrolls back up to the top so we add this little bit of code, that removes the class 
//from navigation link slide 2 and adds it to navigation link slide 1. 
mywindow.scroll(function() { 
    if (mywindow.scrollTop() == 0) { 
     $('.navigation li[data-slide="1"]').addClass('active'); 
     $('.navigation li[data-slide="2"]').removeClass('active'); 
    } 
}); 

//Create a function that will be passed a slide number and then will scroll to that slide using jquerys animate. The Jquery 
//easing plugin is also used, so we passed in the easing method of 'easeInOutQuint' which is available throught the plugin. 
function goToByScroll(dataslide) { 
    htmlbody.animate({ 
     scrollTop: $('.slide[data-slide="' + dataslide + '"]').offset().top 
    }, 2000, 'easeInOutQuint'); 
} 



//When the user clicks on the navigation links, get the data-slide attribute value of the link and pass that variable to the goToByScroll function 
links.click(function (e) { 
    e.preventDefault(); 
    dataslide = $(this).attr('data-slide'); 
    goToByScroll(dataslide); 
}); 

//When the user clicks on the button, get the get the data-slide attribute value of the button and pass that variable to the goToByScroll function 
button.click(function (e) { 
    e.preventDefault(); 
    dataslide = $(this).attr('data-slide'); 
    goToByScroll(dataslide); 

}); 

});

任何幫助非常感謝。

回答

2

您應該看看jQuery Waypoints plugin提供的功能。

第一個是嘗試設置航點插件的偏移量略微設置(http://imakewebthings.com/jquery-waypoints/#doc-disable)。這將使航點觸發在頁面的不同位置,以便即使您位於屏幕的頂部,也會觸發第一個航點。

//Setup waypoints plugin 
slide.waypoint(function (event, direction) { 

dataslide = $(this).attr('data-slide'); 

if (direction === 'down') { 
    $('.navigation li[data-slide="' + dataslide + '"]').addClass('active').prev().removeClass('active'); 
}  
else { 
    $('.navigation li[data-slide="' + dataslide + '"]').addClass('active').next().removeClass('active'); 
} 
}, { offset:50 }); // The waypoint is triggered when the element is 50px from the top of the viewport. 

或者您可以將'active'CSS類添加到第一個元素,然後在滾動上更改它。

希望能幫助你。

+0

感謝,的風格切換實際工作比試圖讓JS在Firefox和IE 8更好地工作。 – Keoki 2013-04-26 16:22:24

0

您也可以只添加HTML中的類,因爲你反正滾動從第一個元素遠離腳本刪除類。