jQuery滚动:检测结束和开始

5

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

我已经实现了 jQuery 滚动选项卡,它运作良好。

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
尝试添加标记,或者提供一个fiddle会更好。 - Mayank
1
将您的代码放在JSFiddle中,您可以使用滚动事件找到scrollLeft位置,因此根据滚动位置使用CSS设置为消失。 - Senthilkumar Govindasamy
@Mayank 问题已更新,代码编辑器已添加。 - user2227400
我对Angular不是太熟悉,这里是一个大部分答案。Plunker - Shikkediel
谢谢@Shikkediel,这也是一个可行的解决方案。 - user2227400
没问题,谢谢回复。也许你仍然可以从中挑选一些东西。 - Shikkediel
1个回答

0

我对Angular不是很熟悉,但我还是尝试着解决了你的问题。

我创建了一个函数来切换上一个选项卡和下一个选项卡按钮,如下所示:

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();
        });
    });

  }


);

我希望这个对你有效 :)

非常感谢,你能把它放到 Plunker 上吗?在我的环境中无法运行。我必须对窗口大小调整做出反应,这个来自 Angular 的代码可以封装它:`angular.element($window).bind('resize', function () {});` - user2227400
上一页可以正常工作,下一页不行。我已经将代码更改为($scrollwidthtotal <= $scrolledwidth),现在它可以正常工作了。请您更新您的答案,这样我就可以标记为正确了。谢谢! - user2227400
好的,它完全可行了,谢谢!在任何人抱怨之前,现在我必须将所有内容带入表单中,因为Angular通常会期望这样做。在Angular中进行DOM操作必须清楚地发生在指令中。 :) - user2227400

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接