2015-04-03 49 views
2

如何实现登陆页面的指令,以便当窗口达到给定的剖面高度时,滚动时会将.fadein类添加到.box元素中?我发现很多jQuery或ScrollMagic.js的例子,但是找不到angularjs的例子。如何使用angularjs构建交互式滚动着陆页?

我添加了基本的例子来说明这个想法:

app = angular.module('myApp', []); 
 
app.directive("scroll", function($window) { 
 
    return function(scope, element, attrs) { 
 
    angular.element($window).bind("scroll", function() { 
 
     scope.fadein = true; 
 
    }); 
 
    }; 
 
});
body, 
 
html, 
 
.container { 
 
    position: relative; 
 
    display: block; 
 
    margin: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 
.box { 
 
    padding: 10px; 
 
    display: none; 
 
} 
 
#example-1 { 
 
    background-color: #FC6E51; 
 
} 
 
#example-2 { 
 
    background-color: #4FC1E9; 
 
} 
 
#example-3 { 
 
    background-color: #F4F4F4; 
 
} 
 
.fadein { 
 
    display: block; 
 
} 
 
.center { 
 
    position: relative; 
 
    top: 50%; 
 
    transform: translateY(-50%); 
 
    text-align: center; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<span ng-app="myApp" scroll> 
 

 
    <section id="example-1" class="container"> 
 
    <div class="box center fadein"> 
 
     <h1 class="">Show me when scrolled to this section</h1> 
 
    </div> 
 
    </section> 
 

 
    <section id="example-2" class="container"> 
 
    <div class="box center"> 
 
     <h1 class="">Show me when scrolled to this section</h1> 
 
    </div> 
 
    </section> 
 

 
    <section id="example-3" class="container"> 
 
    <div class="box center"> 
 
     <h1 class="">Show me when scrolled to this section</h1> 
 
    </div> 
 
    </section> 
 

 
</span>

回答

1

启动与此:

app = angular.module('myApp', []); 
 
app.directive("scroll", function($window) { 
 
    return { 
 
     restrict: 'A', 
 
     link: function(scope, element, attrs) { 
 
      var offsetTop = element[0].offsetTop, 
 
       offsetHeight = element[0].offsetHeight; 
 

 
      checkScene(); 
 

 
      angular.element($window).bind("scroll", function() { 
 
       checkScene();      
 
      }); 
 

 
      function checkScene() { 
 
       var bodyScrollTop = document.body.scrollTop; 
 

 
       if (bodyScrollTop >= offsetTop 
 
        && bodyScrollTop <= offsetTop + offsetHeight) { 
 
        element.addClass('fadein'); 
 
       } else { 
 
        element.removeClass('fadein'); 
 
       } 
 
      } 
 
     } 
 
    }; 
 
});
body, html, .container { 
 
    position: relative; 
 
    display: block; 
 
    margin: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.box { 
 
    padding: 10px; 
 
    display: none; 
 
} 
 

 
#example-1 { 
 
    background-color: #FC6E51; 
 
} 
 

 
#example-2 { 
 
    background-color: #4FC1E9; 
 
} 
 

 
#example-3 { 
 
    background-color: #F4F4F4; 
 
} 
 

 
.fadein .box { 
 
    display: block; 
 
} 
 

 
.center { 
 
    position: relative; 
 
    top: 50%; 
 
    transform: translateY(-50%); 
 
    text-align: center; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<span ng-app="myApp"> 
 
    <section id="example-1" class="container" scroll> 
 
     <div class="box center"> 
 
      <h1 class="">Show me when scrolled to this section</h1> 
 
     </div> 
 
    </section> 
 
    <section id="example-2" class="container" scroll> 
 
     <div class="box center"> 
 
      <h1 class="">Show me when scrolled to this section</h1> 
 
     </div> 
 
    </section> 
 
    <section id="example-3" class="container" scroll> 
 
     <div class="box center"> 
 
      <h1 class="">Show me when scrolled to this section</h1> 
 
     </div> 
 
    </section> 
 
</span>