oblador / angular-scroll

Scrollspy, animated scrollTo and scroll events for angular.js
http://oblador.github.io/angular-scroll/
MIT License
1.48k stars 235 forks source link

Variable offset value for scroll spy #179

Open aneeshd16 opened 8 years ago

aneeshd16 commented 8 years ago

In my use case, I need the offset to be a variable, which can be set at run time. To demonstrate this, I have modified examples/toc.html. I have set the initial offset to 200. If you click on the "Reset Offset to 0" link at top, it sets the offset to 0. However the scroll-spy does not reflect the updated offset.

Am I using the wrong approach, is there an alternative, or is this a new feature? :smile:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
  <meta charset="UTF-8" />
  <!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
  <title>Angular Scroll TOC Example</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style type="text/css">
    html, body {
      margin: 0;
      padding: 0;
      background: #ebebeb;
    }
    body, button {
      font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
    }

    h1 {
      padding-top: 50px;
    }
    img {
      max-width: 100%;
    }
    nav {
      position: fixed;
      z-index: 1;
      top: 0;
      left: 0;
      right: 0;
      background: #fff;
      background: rgba(255, 255, 255, 0.8);
    }
    nav li, nav ul {
      list-style: none;
      margin: 0;
      padding: 0;
      text-align: center
    }
    nav li, nav a {
      display: inline-block;
    }
    nav a {
      padding: 20px;
      color: #333;
      text-decoration: none;
    }
    a:hover {
      background: #fff;
    }
    a.active {
      background: #00AC7F;
      color: #fff;
    }
    .wrap {
      position: relative;
      max-width: 1000px;
      margin: 0 auto;
      padding: 50px 20px;
    }
    section {
      padding: 20px 0 20px 0;
      margin-bottom: 30px;
    }
    footer {
      text-align: center;
    }
    button {
      font-size: 18px;
      border: 0;
      padding: 15px 30px;
      background: #00AC7F;
      color: #fff;
      cursor: pointer;
    }

    #container {
      overflow: auto;
      height: 400px;
      padding: 0;
      margin: 10px 0 20px 0;
    }

  </style>
</head>
<body>
<div class="wrap" ng-controller="MyCtrl">
  <nav class="toc">
    <a ng-click="myOffset = 0">Reset Offset to 0</a>
    <ul>
      <li>
        <a href="#section-1" offset={{myOffset}} du-smooth-scroll du-scrollspy>Section 1</a>
        <ul du-spy-context="section-1">
          <li><a href="#section-1-1" offset={{myOffset}} du-smooth-scroll du-scrollspy>Sub Section 1</a></li>
          <li><a href="#section-1-2" offset={{myOffset}} du-smooth-scroll du-scrollspy>Sub Section 2</a></li>
          <li><a href="#section-1-3" offset={{myOffset}} du-smooth-scroll du-scrollspy>Sub Section 3</a></li>
          <li><a href="#section-1-4" offset={{myOffset}} du-smooth-scroll du-scrollspy>Sub Section 4</a></li>
        </ul>
      </li>
      <li><a href="#section-2" offset={{myOffset}} du-smooth-scroll du-scrollspy>Section 2</a></li>
      <li><a href="#section-3" offset={{myOffset}} du-smooth-scroll du-scrollspy>Section 3</a></li>
      <li><a href="http://github.com/oblador/angular-scroll/">Project on Github</a></li>
    </ul>
  </nav>
  <h1>Angular.js Container Scroll Example</h1>
    <section id="section-1">
      <h2>Section 1</h2>
      <div id="section-1-1">
        <h3>Sub Section 1</h3>
        <p>Bacon ipsum dolor sit amet sausage tail capicola ground round hamburger ham hock. Short ribs pig andouille meatball, pastrami tri-tip fatback ham hock shank kielbasa swine. Rump pancetta jerky kielbasa doner beef ribs tongue hamburger strip steak drumstick andouille shoulder shank flank. Swine drumstick meatball pig beef sausage strip steak.</p>
      </div>
      <div id="section-1-2">
        <h3>Sub Section 2</h3>
        <p>Bacon ipsum dolor sit amet sausage tail capicola ground round hamburger ham hock. Short ribs pig andouille meatball, pastrami tri-tip fatback ham hock shank kielbasa swine. Rump pancetta jerky kielbasa doner beef ribs tongue hamburger strip steak drumstick andouille shoulder shank flank. Swine drumstick meatball pig beef sausage strip steak.</p>
      </div>
      <div id="section-1-3">
        <h3>Sub Section 3</h3>
        <p>Bacon ipsum dolor sit amet sausage tail capicola ground round hamburger ham hock. Short ribs pig andouille meatball, pastrami tri-tip fatback ham hock shank kielbasa swine. Rump pancetta jerky kielbasa doner beef ribs tongue hamburger strip steak drumstick andouille shoulder shank flank. Swine drumstick meatball pig beef sausage strip steak.</p>
      </div>
      <div id="section-1-4">
        <h3>Sub Section 4</h3>
        <p>Bacon ipsum dolor sit amet sausage tail capicola ground round hamburger ham hock. Short ribs pig andouille meatball, pastrami tri-tip fatback ham hock shank kielbasa swine. Rump pancetta jerky kielbasa doner beef ribs tongue hamburger strip steak drumstick andouille shoulder shank flank. Swine drumstick meatball pig beef sausage strip steak.</p>
      </div>
    </section>

    <section id="section-2">
      <h2>Section 2</h2>
      <p>Bacon strip steak ground round, tongue pastrami short ribs pork chop venison turducken sausage sirloin. Flank chicken pork chop capicola turkey turducken cow pork loin biltong meatball drumstick pancetta filet mignon ground round fatback. Ham hock jerky short ribs brisket. Meatloaf shoulder pork chop capicola, sirloin swine pig pork. Jerky ribeye hamburger pork loin sirloin kevin bresaola boudin chuck flank. Ham hock pork belly chicken jerky rump bresaola.</p>
    </section>

    <section id="section-3">
      <h2>Section 3</h2>
      <p>Shank fatback pastrami short loin, turkey jowl kielbasa ribeye chicken jerky drumstick flank ham. Swine shankle pork belly kielbasa shoulder flank jowl, sirloin doner. Kevin tri-tip bresaola leberkas. Swine ball tip cow strip steak. Ham filet mignon pork chop, pork fatback andouille pork loin shoulder jowl swine strip steak turducken prosciutto rump.</p>

      <p>Tongue tri-tip pastrami, shoulder rump pork belly ground round. Ham hock chuck leberkas doner, strip steak corned beef tri-tip capicola. Rump turkey ham sausage shankle. Flank shankle pork chop ham hock. Shankle venison kielbasa, pancetta swine beef ball tip t-bone bacon hamburger ground round ribeye flank. Turducken bacon bresaola, chicken kevin boudin ball tip strip steak filet mignon pork turkey shank ground round. Kielbasa fatback prosciutto pork chop, jerky ground round leberkas boudin ball tip beef shankle shoulder swine brisket. </p>
    </section>

    <section id="section-4">
      <h2>Section 4</h2>

      <p><a href="#section-1" offset={{myOffset}} du-smooth-scroll>To section 1</a> Shoulder cow tenderloin chuck, pork chop jerky doner leberkas. Chuck sausage hamburger, kevin beef pork chop pork shoulder ground round ball tip turducken flank. Bresaola tri-tip meatloaf, salami venison tail pig shank shankle jowl sausage brisket cow biltong turducken. Swine turducken hamburger ball tip short loin prosciutto kevin jowl tri-tip. Doner meatloaf pork brisket.</p>
    </section>
  </div>
  <footer>
    <button ng-click="toTheTop()">To the top!</button> or <button ng-click="toSection2()">To section 2</button>
  </footer>
</div>

<script src="../bower_components/angular/angular.min.js"></script>
<script src="../angular-scroll.js"></script>
<script>
angular.module('myApp', ['duScroll']).
  controller('MyCtrl', function($scope){
    var container = angular.element(document.getElementById('container'));
    var section2 = angular.element(document.getElementById('section-2'));
    $scope.myOffset = 200;
    $scope.toTheTop = function() {
      container.scrollTop(0, 5000);
    }

    $scope.toSection2 = function() {
      container.scrollTo(section2, 0, 1000);
    }
  }
);
</script>
</body>
</html>
seidme commented 8 years ago

+1

Djangofs commented 8 years ago

I have the same issue, any changes to offset are not taken into account by scrollspy