奥门金沙手机娱乐网址 2

jQuery完毕单行文字间歇无缝向上滚动作效果应-Web前端之家奥门金沙手机娱乐网址

用jQuery实现四个方向的滚动代码,搜索下,发现各式各样的都有,我自己也总结了下,分享一个通用的向上滚动代码,间歇向上无缝滚动。如果你想实现向其他三个方向滚动,直接可以改代码即可,这里就不啰嗦了。直接进入干货环节。

版本记录

版本号 时间
V1.0 2017.05.31

奥门金沙手机娱乐网址 1

JS代码:

前言

ios中又很多实用的小技巧,实现不难很实用,以后我会慢慢的给大家总结。这一篇主要讲述滚动方向的判断。
  滚动视图是app很常用的视图形式,而其中scrollView及其子类UITableView和UICollectionView用的很多,在用的时候有一个需求就是:需要判断当前的滚动方向

  • 向上滚动
  • 向下滚动
  • 向左滚动
  • 向右滚动
    下面就以两个例子对滚动方向进行判断,下面我们开始。

123.gif

functionAutoScroll.find.animate({marginTop:"-25px"},500,function.css({marginTop:"0px"}).find.appendTo;}${setInterval('AutoScroll("#scrollCom")',4000);});

详情

要实现如上图的效果,红色区域是筛选条件的区域,随着想向上滚动并且停留在顶部。

HTML代码:

jQuery完毕单行文字间歇无缝向上滚动作效果应-Web前端之家奥门金沙手机娱乐网址。一、左右滚动方向的判断

下面我们就以UIScrollView为例子,说明左右滚动是如何判断的,还是先看一下代码组成。

奥门金沙手机娱乐网址 2

代码组成

第一感觉是想用CoordinatorLayout.Behavior
来实现,将Toolbar+图片区域+红色筛选区域作为折叠的头部。但是这样SwipeRefreshLayout的下拉圈圈就不是从Toolbar下面下拉出来的。

1、2月份以来,金价已从最低1122涨到最高1263,每日均有行情。2、机构预测黄金将是2016年超级英雄,金价有望升至1400美元。

思路引导

下面我们看,当我们向左滚动时可见,x为正值且越来越大,如下:

2017-06-01 00:15:47.962 scrollDirectionJudge[2981:77203] x==0.333333,y==-64.000000
2017-06-01 00:15:48.026 scrollDirectionJudge[2981:77203] x==1.000000,y==-64.000000
2017-06-01 00:15:48.057 scrollDirectionJudge[2981:77203] x==1.333333,y==-64.000000
2017-06-01 00:15:48.089 scrollDirectionJudge[2981:77203] x==1.666667,y==-64.000000
2017-06-01 00:15:48.122 scrollDirectionJudge[2981:77203] x==2.000000,y==-64.000000
2017-06-01 00:15:48.145 scrollDirectionJudge[2981:77203] x==4.000000,y==-64.000000
2017-06-01 00:15:48.170 scrollDirectionJudge[2981:77203] x==7.666667,y==-64.000000
2017-06-01 00:15:48.194 scrollDirectionJudge[2981:77203] x==11.000000,y==-64.000000
2017-06-01 00:15:48.218 scrollDirectionJudge[2981:77203] x==12.666667,y==-64.000000
2017-06-01 00:15:48.235 scrollDirectionJudge[2981:77203] x==13.000000,y==-64.000000
2017-06-01 00:15:48.956 scrollDirectionJudge[2981:77203] x==13.666667,y==-64.000000
2017-06-01 00:15:49.036 scrollDirectionJudge[2981:77203] x==14.000000,y==-64.000000

反过来,如果我们向右滑动,可见

2017-06-01 00:17:27.177 scrollDirectionJudge[2981:77203] x==412.000000,y==-64.000000
2017-06-01 00:17:27.200 scrollDirectionJudge[2981:77203] x==407.666667,y==-64.000000
2017-06-01 00:17:27.216 scrollDirectionJudge[2981:77203] x==405.333333,y==-64.000000
2017-06-01 00:17:27.240 scrollDirectionJudge[2981:77203] x==402.333333,y==-64.000000
2017-06-01 00:17:27.265 scrollDirectionJudge[2981:77203] x==401.333333,y==-64.000000
2017-06-01 00:17:27.288 scrollDirectionJudge[2981:77203] x==401.000000,y==-64.000000
2017-06-01 00:17:27.353 scrollDirectionJudge[2981:77203] x==400.666667,y==-64.000000
2017-06-01 00:17:27.377 scrollDirectionJudge[2981:77203] x==399.666667,y==-64.000000
2017-06-01 00:17:27.400 scrollDirectionJudge[2981:77203] x==397.666667,y==-64.000000
2017-06-01 00:17:27.416 scrollDirectionJudge[2981:77203] x==395.666667,y==-64.000000
2017-06-01 00:17:27.432 scrollDirectionJudge[2981:77203] x==393.333333,y==-64.000000
2017-06-01 00:17:27.448 scrollDirectionJudge[2981:77203] x==391.666667,y==-64.000000
2017-06-01 00:17:27.473 scrollDirectionJudge[2981:77203] x==390.333333,y==-64.000000

那么,我们可见,可以有这个思路,如果我们记录当前的x偏移值,并且用下一时刻临近的x偏移值与之相比较,取差值,如果**下一时刻的x偏移值

  • 当前x偏移值**为正值,则是向左边滑动,反之则为向右滑动。

看输出结果,先看向左滑动

2017-06-01 00:24:38.629 scrollDirectionJudge[3185:84739] x==4.666667,y==-64.000000
2017-06-01 00:24:38.629 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.652 scrollDirectionJudge[3185:84739] x==10.666667,y==-64.000000
2017-06-01 00:24:38.652 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.676 scrollDirectionJudge[3185:84739] x==15.666667,y==-64.000000
2017-06-01 00:24:38.677 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.700 scrollDirectionJudge[3185:84739] x==20.000000,y==-64.000000
2017-06-01 00:24:38.700 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.726 scrollDirectionJudge[3185:84739] x==22.666667,y==-64.000000
2017-06-01 00:24:38.727 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.747 scrollDirectionJudge[3185:84739] x==24.333333,y==-64.000000
2017-06-01 00:24:38.748 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.764 scrollDirectionJudge[3185:84739] x==25.000000,y==-64.000000
2017-06-01 00:24:38.765 scrollDirectionJudge[3185:84739] 正在向左滑动
2017-06-01 00:24:38.792 scrollDirectionJudge[3185:84739] x==25.666667,y==-64.000000
2017-06-01 00:24:38.792 scrollDirectionJudge[3185:84739] 正在向左滑动

在看向右滑动

2017-06-01 00:25:58.949 scrollDirectionJudge[3185:84739] x==27.333333,y==-64.000000
2017-06-01 00:25:58.950 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:58.970 scrollDirectionJudge[3185:84739] x==24.333333,y==-64.000000
2017-06-01 00:25:58.970 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:58.986 scrollDirectionJudge[3185:84739] x==22.666667,y==-64.000000
2017-06-01 00:25:58.986 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.002 scrollDirectionJudge[3185:84739] x==21.333333,y==-64.000000
2017-06-01 00:25:59.003 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.018 scrollDirectionJudge[3185:84739] x==20.000000,y==-64.000000
2017-06-01 00:25:59.019 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.034 scrollDirectionJudge[3185:84739] x==19.333333,y==-64.000000
2017-06-01 00:25:59.035 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.054 scrollDirectionJudge[3185:84739] x==18.666667,y==-64.000000
2017-06-01 00:25:59.054 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.074 scrollDirectionJudge[3185:84739] x==16.000000,y==-64.000000
2017-06-01 00:25:59.074 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.090 scrollDirectionJudge[3185:84739] x==14.000000,y==-64.000000
2017-06-01 00:25:59.091 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.115 scrollDirectionJudge[3185:84739] x==11.666667,y==-64.000000
2017-06-01 00:25:59.115 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.138 scrollDirectionJudge[3185:84739] x==10.666667,y==-64.000000
2017-06-01 00:25:59.139 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.195 scrollDirectionJudge[3185:84739] x==10.000000,y==-64.000000
2017-06-01 00:25:59.196 scrollDirectionJudge[3185:84739] 正在向右滑动
2017-06-01 00:25:59.251 scrollDirectionJudge[3185:84739] x==9.666667,y==-64.000000
2017-06-01 00:25:59.251 scrollDirectionJudge[3185:84739] 正在向右滑动

我尝试过自己用CoordinatorLayout.Behavior来实现这这个效果,而且上下滚动的效果已经实现了,但是写到下拉刷新处的时候,我暂时放弃了,我想到了另外一个方法,就是仿CoordinatorLayout.Behavior
来拦截SwipeRefreshLayout
的nestedscroll事件,因为SwipeRefreshLayout也继承了NestedScrollingParent。

如果你有什么问题可以 留言给我。

代码实现

下面我们就直接看代码的实现。

JJHorizontalDirectionVC.m

#import "JJHorizontalDirectionVC.h"

@interface JJHorizontalDirectionVC () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *textScrollView;
@property (nonatomic, assign) CGFloat lastOffSetX;

@end

@implementation JJHorizontalDirectionVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setupUI];
}

#pragma mark - Object Private Function

- (void)setupUI
{
    UIScrollView *textScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    textScrollView.backgroundColor = [UIColor greenColor];
    textScrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, 0);
    textScrollView.delegate = self;
    [self.view addSubview:textScrollView];
    self.textScrollView = textScrollView;

}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"x==%f,y==%f",scrollView.contentOffset.x,scrollView.contentOffset.y);

    if (scrollView.contentOffset.x - self.lastOffSetX > 0) {
        NSLog(@"正在向左滑动");
    }
    else {
        NSLog(@"正在向右滑动");
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.lastOffSetX = scrollView.contentOffset.x;
}

@end

public class MySwipeRefreshLayout extends SwipeRefreshLayout {
    private static final String TAG = "MySwipeRefreshLayout";
    private static  int HEADER_EXPEND_HEIGTH =0;
    private  View rootScrollLayout;
    private View mScrollUpChild;
    private int curentTranslationY;

    public MySwipeRefreshLayout(Context context) {
        this(context, null);
    }

    public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        HEADER_EXPEND_HEIGTH = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, context.getResources().getDisplayMetrics());
        curentTranslationY = HEADER_EXPEND_HEIGTH;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        rootScrollLayout = findViewById(R.id.rootScrollLayout);
        Log.i(TAG, "MySwipeRefreshLayout: rootScrollLayout=" + rootScrollLayout);
        rootScrollLayout.setTranslationY(curentTranslationY);

    }

    @Override
    public boolean canChildScrollUp() {
        if (mScrollUpChild != null) {
            return ViewCompat.canScrollVertically(mScrollUpChild, -1);
        }
        return super.canChildScrollUp();
    }

    public void setScrollUpChild(View view) {
        mScrollUpChild = view;
    }


    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        Log.i(TAG, "onStartNestedScroll: child="+child);
        //接收垂直方向的滚动事件,SwipeRefreshLayout默认是,只要正在刷新,父控件就不接受嵌套事件,由scrollview自己内部消耗
        return isEnabled() &&  (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
//        return super.onStartNestedScroll(child, target, nestedScrollAxes);
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        if (dy > 0) {//向上滚动
            if (curentTranslationY>=0) {//可以整体先向上滚动
                int min = (int) Math.min(rootScrollLayout.getTranslationY(), dy);
                curentTranslationY = (int) (rootScrollLayout.getTranslationY() - min);
                rootScrollLayout.setTranslationY(curentTranslationY);
                consumed[1] = min;
            }
        }
        super.onNestedPreScroll(target, dx, dy, consumed);
        Log.i(TAG, "onNestedPreScroll: ");
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {

        int max = 0;
        if (dyConsumed <= 0 && dyUnconsumed < 0) {//向下滚动
            if (curentTranslationY<HEADER_EXPEND_HEIGTH) {//可以整体先向下滚动
                 max = (int) Math.max(rootScrollLayout.getTranslationY()-HEADER_EXPEND_HEIGTH, dyUnconsumed);
                curentTranslationY = (int) (rootScrollLayout.getTranslationY() - max);
                rootScrollLayout.setTranslationY(curentTranslationY);
            }
            if (isRefreshing()) {//如果正在更新,则不显示让SwipeRefreshLayout处理,否则会二度下拉正在更新
                return;
            }
        }
        //如果还有多余的需要SwipeRefreshLayout 处理,比如再下拉显示正在更新
        super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed-max);
        Log.i(TAG, "onNestedScroll: ");
    }

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        Log.i(TAG, "drawChild: child=" + child);
        return super.drawChild(canvas, child, drawingTime);
    }
}