2016-02-23 40 views
8

我試圖實現一個滑動水平佈局與標題橫幅。滑動水平響應佈局和MouseWheel輸入

這是HTML結構:

<body> 
    <div id="header"> 
    <div><a href="#one"> one </a></div> 
    <div><a href="#two"> two </a></div> 
    <div><a href="#thr"> thr </a></div> 
    </div> 

    <div id="one" class="panel"> </div> 
    <div id="two" class="panel"> </div> 
    <div id="thr" class="panel"> </div> 
</body> 

頭是固定的,板已經被設置有梯度的背景(中間面板具有不同的顏色用於調試目的)。 這裏是CSS:

body { 
     width: 6000px; 
     overflow: hidden; 
    } 

    .panel { 
     width: 33.3%; 
     float: left; 
     padding-left: 30px; 
     padding-right: 1040px; 
     margin-top: -75px; 
     height: 960px; 
     background-image: linear-gradient(to bottom, #0B88FF, #95EEFF); 
    } 

    #header { 
     position: fixed; 
     height: 75px; 
     margin-top: 25px; 
    } 

    #two{ 
     background-image: linear-gradient(to bottom, #0B8800, #9E5EFF); 
    } 

最後的功能,管理面板之間的動畫:

$(document).ready(function() { 
    $("#header a").bind("click", function(event) { 
    event.preventDefault(); 
    var target = $(this).attr("href"); 
    $("html, body").stop().animate({ 
     scrollLeft: $(target).offset().left, 
     scrollTop: $(target).offset().top 
    }, 1200); 
    }); 
}); 

我所面臨的問題有以下幾點:

1)我試着實現一個jQuery函數來在用戶使用鼠標滾輪時運行幻燈片動畫,但是我的測試沒有任何工作...結構總是相同的:

$(window).scroll(function() { 
     if ($(this).scrollTop() > 0) { 
     var target // still not able to figure out who should i target 
     $("html, body").stop().animate({ 
      //to the target >,< 
     } 
}); 

2)當我的瀏覽器窗口是在100%大小的一切似乎運作良好,但如果我減少或增加變焦一切都搞砸了>,< 我注意到它可以處理這個問題,並here is an example:

回答

2

爲了得到你的目標,你可以用類panel這個元素填充一個數組,然後用一個索引來移動面板。

最後,如果你有對窗口大小調整滾動的問題,你可以結合這event,做任何你想要

看看MouseWheelEvent

,並嘗試與您的代碼這個例子:

$(document).ready(function() { 
 
    $("#header a").bind("click", function(event) { 
 
    event.preventDefault(); 
 
    var target = $(this).attr("href"); 
 
    $("html, body").stop().animate({ 
 
     scrollLeft: $(target).offset().left, 
 
     scrollTop: $(target).offset().top 
 
    }, 1200); 
 
    }); 
 
    
 
    var scroll_targets = $(".panel"); 
 
    var scroll_targets_index = 0; 
 
    $(window).on('DOMMouseScroll mousewheel', function (e) {  
 
    if (e.originalEvent.wheelDelta < 0) { 
 
     if(scroll_targets_index < scroll_targets.length-1){ 
 
     var where = ++scroll_targets_index; 
 
     $("html, body").stop().animate({ 
 
      scrollLeft: $(scroll_targets[where]).offset().left, 
 
      scrollTop: $(scroll_targets[where]).offset().top 
 
     }, 1200); 
 
     } 
 
    } 
 
    else { 
 
    \t var where; 
 
    \t if(scroll_targets_index > 0){ 
 
     \t where = --scroll_targets_index; 
 
     } 
 
     else{ 
 
     \t where = 0; 
 
     } 
 
     \t $("html, body").stop().animate({ 
 
      scrollLeft: $(scroll_targets[where]).offset().left, 
 
      scrollTop: $(scroll_targets[where]).offset().top 
 
     }, 1200); 
 
     
 
    } 
 
    }); 
 
    
 
    $(window).resize(function() { 
 
    $('html,body').scrollTop($(scroll_targets[where]).offset().top); 
 
    $('html,body').scrollLeft($(scroll_targets[where]).offset().left); 
 
    }); 
 
});
#body { 
 
     width: 6000px; 
 
     overflow: hidden; 
 
    } 
 

 
    .panel { 
 
     width: 33.3%; 
 
     float: left; 
 
     padding-left: 30px; 
 
     padding-right: 1040px; 
 
     margin-top: -75px; 
 
     height: 960px; 
 
     background-image: linear-gradient(to bottom, #0B88FF, #95EEFF); 
 
    } 
 

 
    #header { 
 
     position: fixed; 
 
     height: 75px; 
 
     margin-top: 25px; 
 
    } 
 

 
    #two{ 
 
     background-image: linear-gradient(to bottom, #0B8800, #9E5EFF); 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="body"> 
 
    <div id="header"> 
 
    <div><a href="#one"> one </a></div> 
 
    <div><a href="#two"> two </a></div> 
 
    <div><a href="#thr"> thr </a></div> 
 
    </div> 
 

 
    <div id="one" class="panel"> </div> 
 
    <div id="two" class="panel"> </div> 
 
    <div id="thr" class="panel"> </div> 
 
</div>

1

在解開,我認爲這是與CSS和JavaScript使用的Transform3D和其他一些CSS技巧製成。如果你想獲得滾動事件,當你有溢出時它不會發生:隱藏在主體上,因爲你剛剛告訴瀏覽器隱藏它的所有滾動條!所以,我想如何解決現在遇到的問題,是使用鼠標滾輪事件,就像這樣:

$(window).on('wheel', function (event) {  
    console.log(event); // Here you can see all of its events properties and functions in the console 
}); 

有一個wheelDelta屬性,它會返回120,如果你正在向前做一個鼠標滾輪或-120向後做一個鼠標滾輪的時候,所以我連着一個計數器,這樣我可以知道鼠標滾輪事件多少次觸發:

$(window).on('wheel', function (event) {  

    if (event.originalEvent.wheelDelta/120 > 0) { 
     count++; 
     console.log(count) ; 
     if(count > 30){ 

      $("html, body").stop().animate({ 
       scrollLeft: $('#two').offset().left, 
       scrollTop: $('#two').offset().top 
      }, 1200); 
     } 
     if(count > 60){ 

      $("html, body").stop().animate({ 
       scrollLeft: $('#thr').offset().left, 
       scrollTop: $('#thr').offset().top 
      }, 1200);    
     } 
     if(count > 90){ 

      $("html, body").stop().animate({ 
       scrollLeft: $('#two').offset().left, 
       scrollTop: $('#two').offset().top 
      }, 1200); 
      count = 0;   
     } 


    } 
}); 

這是隻爲你繼續創建邏輯建立在鼠標滾輪將改爲另一個面板等等,祝你好運!