ionic實現(xiàn)滑動的三種方式
在移動端受屏幕大小所限,展示內(nèi)容很多的時候,就要使部分區(qū)域進行滑動。本文展示項目中所有到的幾種方式,大家可以看自己的需求選擇合適的滑動方式。實現(xiàn)滑動的基本原理,有兩個容器A、B,假如A在外層,B在內(nèi)層;外層的A寬度或高度固定,內(nèi)層容器B寬度或者高度大于A即可實現(xiàn)滾動。
實現(xiàn)方式
1). ion-scroll
利用ionic自帶的滑動指令
<ion-view view-title="Dashboard">
<ion-content class="padding" has-bouncing="false">
<ion-scroll has-bouncing="false" style="width:100px;border:solid 1px red;" direction="x">
<div style="width:200px;">
ion-scroll實現(xiàn)滑動,用ionic自帶的滑動組件實現(xiàn)滑動,ion-scroll其他屬性可參考官網(wǎng)文檔
</div>
</ion-scroll>
</ion-content>
</ion-view>
2). css的overflow
<ion-view view-title="Dashboard">
<ion-content class="padding" has-bouncing="false" overflow-scroll="true">
<div style="width:160px;height:300px;border:solid 1px red;overflow: auto;padding:20px;">
<div style="width:400px;height:500px;border:solid 1px blueviolet">
使用css的overflow屬性atuo或者scroll實現(xiàn)滾動,使用css的overflow屬性atuo或者scroll實現(xiàn)滾動
使用css的overflow屬性atuo或者scroll實現(xiàn)滾動,使用css的overflow屬性atuo或者scroll實現(xiàn)滾動
使用css的overflow屬性atuo或者scroll實現(xiàn)滾動,使用css的overflow屬性atuo或者scroll實現(xiàn)滾動
</div>
</div>
</ion-content>
</ion-view>
•overflow:auto 如果內(nèi)容被修剪,則瀏覽器會顯示滾動條以便查看其余的內(nèi)容。
•overflow:scroll內(nèi)容會被修剪,但是瀏覽器會顯示滾動條以便查看其余的內(nèi)容。
注:使用這種方式,ion-content需要添加overflow-scroll="true"指令,表示使用系統(tǒng)自己的滾動來代替ionic的scroll,ionic是實現(xiàn)了自己的一套滾動方式的。
監(jiān)聽touch事件
<div style="width:100%;border:solid 1px;height:60px;overflow: hidden;white-space:nowrap;padding:10px 20px;" id="dash_scroll_container">
<div ng-repeat="d in datas" style="margin-right:20px;border:solid 1px #FFC900;height:100%;width:100px;display:inline-block;text-align:center;line-height:40px;">
{ublnpf9mb}
</div>
</div>
對應(yīng)的js
angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope) {
$scope.datas=[1,2,3,4,5,6,7,8,9,10];
var startX=0,startY=0;
var $domScroll=$("#dash_scroll_container");
$domScroll.on("touchstart",function(e){
startX=e.originalEvent.changedTouches[0].pageX;
startY=e.originalEvent.changedTouches[0].pageY;
console.log("start:("+startX+","+startY+")"+"--"+$(this).scrollTop());
});
$domScroll.on("touchmove",function(e){
var x = e.originalEvent.changedTouches[0].pageX-startX;
var y = e.originalEvent.changedTouches[0].pageY-startY;
if ( Math.abs(x) > Math.abs(y)) {//左右滑動
scrollLeft($(this),x);
}else if( Math.abs(y) > Math.abs(x)){//上下滑動
scrollTop($(this),y);
}
function scrollLeft(obj,x){
var currentScroll = obj.scrollLeft();
console.log(parseInt(currentScroll)-x);
obj.scrollLeft(parseInt(currentScroll)-x);//滑動的距離
e.preventDefault();
e.stopPropagation();
}
function scrollTop(obj,y){
var currentScroll = obj.scrollTop();
console.log(parseInt(currentScroll)-y);
obj.scrollTop(parseInt(currentScroll)-y);//滑動的距離
e.preventDefault();
e.stopPropagation();
}
});
})
通過監(jiān)聽手指的touchstart、touchmove事件,獲得滑動的距離,調(diào)用外部容器div的滾動條滾動到對應(yīng)距離。
•$(selector).scrollLeft(position):設(shè)置元素的水平滾動位置
•$(selector).scrollTop(position):設(shè)置元素的垂直滾動位置
最后,再使用angularjs的指令,講滾動的監(jiān)聽封裝為一個指令,方便以后滑動時候使用。
在directive.js中添加:
angular.module('starter.directives', [])
.directive('myScroll',function(){
function link($scope, element, attrs) {
var $domScroll=$(element[0]);
var startX=0,startY=0;
$domScroll.on("touchstart",function(e){
startX=e.originalEvent.changedTouches[0].pageX;
startY=e.originalEvent.changedTouches[0].pageY;
console.log("start:("+startX+","+startY+")"+"--"+$(this).scrollTop());
});
$domScroll.on("touchmove",function(e){
var x = e.originalEvent.changedTouches[0].pageX-startX;
var y = e.originalEvent.changedTouches[0].pageY-startY;
if ( Math.abs(x) > Math.abs(y)) {//左右滑動
scrollLeft($(this),x);
}else if( Math.abs(y) > Math.abs(x)){ //上下滑動
scrollTop($(this),y);
}
function scrollLeft(obj,x){
var currentScroll = obj.scrollLeft();
console.log(parseInt(currentScroll)-x);
obj.scrollLeft(parseInt(currentScroll)-x);//滑動的距離
e.preventDefault();
e.stopPropagation();
}
function scrollTop(obj,y){
var currentScroll = obj.scrollTop();
console.log(parseInt(currentScroll)-y);
obj.scrollTop(parseInt(currentScroll)-y);//滑動的距離
e.preventDefault();
e.stopPropagation();
}
});
}
return {
restrict: 'A',
link: link
};
});
這樣封裝后使用起來就方便了,在需要滑動的元素上加上指令 my-scroll。
<div my-scroll style="border:slateblue solid 1px;width:100%;height:300px;overflow: hidden;margin:0;padding:0;" class="row">
<div class="col-20">
<div ng-repeat="d in datas" style="margin-bottom:20px;border:solid 1px #007AFF;height:80px;text-align:center;line-height:80px;">
地區(qū){ublnpf9mb}
</div>
</div>
<div class="col-80">
<div style="width:200%;border:solid 1px #009689;overflow: hidden;white-space: nowrap;">
<div ng-repeat="d in datas" style="margin-bottom:20px;border:solid 1px #FFC900;height:80px;text-align:center;line-height:80px;">
{ublnpf9mb}每行
</div>
</div>
</div>
</div>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+Pinia+TypeScript?實現(xiàn)封裝輪播圖組件
這篇文章主要介紹了vue3+Pinia+TypeScript?實現(xiàn)封裝輪播圖組件,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
RGB轉(zhuǎn)換實現(xiàn)代碼,淘寶前端開發(fā)工程師筆試題
寫一個轉(zhuǎn)換RGB的值的函數(shù),實現(xiàn)以下效果。2010-11-11
分享10個優(yōu)化代碼的CSS和JavaScript工具
如果你想在保持文件的時候或執(zhí) 行的階段lint代碼,那么linting工具也可以如你所愿。這取決于個人的選擇。如果你正在找尋用于CSS和JavaScript最好的 linting工具,那么請繼續(xù)閱讀2016-05-05

