jQuery Scroll:检测结束并开始

时间:2015-11-18 08:58:29

标签: javascript jquery html angularjs

编辑>> Plunker:http://plnkr.co/edit/LY7LUAylvKQ3pIv9lhYM?p=preview

我已经为Tab-Titles实现了jQuery Scroll,它运行良好。

enter image description here

如果我在开头,左侧的箭头应该消失,当我向右移动时,它应该显示出来。如果我在最后,右侧的箭头应该消失。

我如何检测起点和终点?我希望能够对窗口调整大小做出反应。

这些是按钮:

 $('#nextTabBtn').click(function () {
     var $target = $('.tabBoxMantle');
     if ($target.is(':animated')) return;
     $target.animate({
         scrollLeft: $target.scrollLeft() + 300
     }, 800);
 });


 $('#prevTabBtn').click(function () {
     var $target = $('.tabBoxMantle');
     if ($target.is(':animated')) return;
     $target.animate({
         scrollLeft: $target.scrollLeft() - 300
     }, 800);
 });

1 个答案:

答案 0 :(得分:0)

我对Angular不太熟悉,但我仍尝试修复你的问题

我创建了一个功能来切换Prev Tab和Next Tab Button,如下所示

function ShowPreviousTabBtn() { // handles prev tab button visibility
  var $targetScroll = $('.auTabBoxMantle');
  if ($targetScroll.scrollLeft() == 0)
    $('#prevTabBtn').hide();
  else
    $('#prevTabBtn').show();
}
function ShowNextTabBtn() { // handles next tab button visibility
  var $targetScroll = $('.auTabBoxMantle');
  var $scrollwidthtotal = $targetScroll[0].scrollWidth;
  var $scrolledwidth = $targetScroll.width() + $targetScroll.scrollLeft();
  if ($scrollwidthtotal <= $scrolledwidth)
    $('#nextTabBtn').hide();
  else
    $('#nextTabBtn').show();
}

app.js的完整工作代码如下所示

angular.module('myApp', [])

.controller(

  "MainController",
  function($scope, $http, $window, $document, $location, $anchorScroll) {

    $scope.my = {};
    $scope.my.title = "jQuery Scroll Example";


    $http.get('data.json')
      .success(function(data) {
        $scope.my.tabs = data;
      });

    function ShowPreviousTabBtn() {
      var $targetScroll = $('.auTabBoxMantle');
      if ($targetScroll.scrollLeft() == 0)
        $('#prevTabBtn').hide();
      else
        $('#prevTabBtn').show();
    }

    function ShowNextTabBtn() {
      var $targetScroll = $('.auTabBoxMantle');
      var $scrollwidthtotal = $targetScroll[0].scrollWidth;
      var $scrolledwidth = $targetScroll.width() + $targetScroll.scrollLeft();
      if ($scrollwidthtotal == $scrolledwidth)
        $('#nextTabBtn').hide();
      else
        $('#nextTabBtn').show();
    }

    // Initially hide the previous Tab button
    ShowPreviousTabBtn();
    // Hiding the Next Tab if the Items div is not overflown
    ShowNextTabBtn();

    $('#nextTabBtn').click(function() {

      var $target = $('.auTabBoxMantle');
      if ($target.is(':animated')) return;
      $target.animate({
        scrollLeft: $target.scrollLeft() + 300
      }, 800)
      .promise().done(function() { // used as a callback function for animate
        ShowPreviousTabBtn();
        ShowNextTabBtn();
      });

    });

    $('#prevTabBtn').click(function() {
      var $target = $('.auTabBoxMantle');
      if ($target.is(':animated')) return;
      $target.animate({
          scrollLeft: $target.scrollLeft() - 300
        }, 800)
      .promise().done(function() { // used as a callback function for animate
          ShowPreviousTabBtn();
          ShowNextTabBtn();
        });
    });

  }


);

我希望这适合你:)