html纵向固定导航菜单代码,jQuery和css3响应式垂直固定导航菜单插件
這是一款非常實用的jQuery和css3響應式垂直固定導航菜單插件。當你的頁面上有很多的內容,用戶需要花費大量的時間才能找到他們想要的內容。這個垂直固定導航菜單插件能夠為頁面提供一個內容預覽,使用戶能非常輕松的找到他們需要的內容。
HTML結構
導航菜單使用nav作為wrapper,并為每一個導航項創建一個section。另外添加了一個trigger用于在觸摸屏設備上導航。
Open navigation
Item 1
CSS樣式
我們使用Modernizr(.touch和.no-touch)來檢測觸摸和非觸摸設備,并提供兩個自定義的導航方法。因此,你在大屏幕上不論如何縮小瀏覽器,看到的都是小點導航按鈕,但是如果觸摸屏設備來查看這個demo,你會看到下圖所示的樣子:
在非觸摸屏的設備上,我們將trigger隱藏,并給元素設置position: fixed。
默認情況下,我們給導航菜單項設置scale-down的transform效果。在鼠標滑過它們時,在使它們scale-up起來。
.no-touch #cd-vertical-nav {
/*fix the navigation*/
position: fixed;
right: 40px;
top: 50%;
bottom: auto;
transform: translateY(-50%);
}
.no-touch #cd-vertical-nav a span {
float: right;
/*scale down navigation dots and labels*/
transform: scale(0.6);
}
.no-touch #cd-vertical-nav .cd-dot {
transform-origin: 50% 50%;
}
.no-touch #cd-vertical-nav .cd-label {
transform-origin: 100% 50%;
}
.no-touch #vertical-nav a:hover span {
/*scale up navigation dots and labels*/
transform: scale(1);
}
.no-touch #cd-vertical-nav a:hover .cd-label {
/*show labels*/
opacity: 1;
}
在移動觸摸設備上,我們為.cd-nav-trigger和設置position: fixed。
當用戶點擊了.cd-nav-trigger元素,我們給導航菜單添加.open類,用來改變CSS3 scale的值從0變到1,并通過CSS3 transition使動畫更加平滑。
下面是一些簡化代碼:
.touch #cd-vertical-nav {
position: fixed;
z-index: 1;
right: 5%;
bottom: 30px;
width: 90%;
max-width: 400px;
max-height: 90%;
transform: scale(0);
transition-property: transform;
transition-duration: 0.2s;
}
.touch #cd-vertical-nav.open {
transform: scale(1);
}
JAVASCRIPT
當用戶向下滾動鼠標,updateNavigation()方法會計算出哪一個是當前瀏覽的section,并未相應的導航菜單項添加.is-selected類(基于導航菜單項的data-number屬性)。
jQuery(document).ready(function($){
var contentSections = $('.cd-section'),
navigationItems = $('#cd-vertical-nav a');
updateNavigation();
$(window).on('scroll', function(){
updateNavigation();
});
//smooth scroll to the section
navigationItems.on('click', function(event){
event.preventDefault();
smoothScroll($(this.hash));
});
//smooth scroll to second section
$('.cd-scroll-down').on('click', function(event){
event.preventDefault();
smoothScroll($(this.hash));
});
//open-close navigation on touch devices
$('.touch .cd-nav-trigger').on('click', function(){
$('.touch #cd-vertical-nav').toggleClass('open');
});
//close navigation on touch devices when selectin an elemnt from the list
$('.touch #cd-vertical-nav a').on('click', function(){
$('.touch #cd-vertical-nav').removeClass('open');
});
function updateNavigation() {
contentSections.each(function(){
$this = $(this);
var activeSection = $('#cd-vertical-nav a[href="#'+$this.attr('id')+'"]').data('number') - 1;
if ( ( $this.offset().top - $(window).height()/2 < $(window).scrollTop() ) && ( $this.offset().top + $this.height() - $(window).height()/2 > $(window).scrollTop() ) ) {
navigationItems.eq(activeSection).addClass('is-selected');
}else {
navigationItems.eq(activeSection).removeClass('is-selected');
}
});
}
function smoothScroll(target) {
$('body,html').animate(
{'scrollTop':target.offset().top},
600
);
}
});
總結
以上是生活随笔為你收集整理的html纵向固定导航菜单代码,jQuery和css3响应式垂直固定导航菜单插件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win7系统桌面右下角托盘图标不显示原因
- 下一篇: 2017年html5行业报告,云适配发布