ionic實(shí)現(xiàn)滑動的三種方式
在移動端受屏幕大小所限,展示內(nèi)容很多的時(shí)候,就要使部分區(qū)域進(jìn)行滑動。本文展示項(xiàng)目中所有到的幾種方式,大家可以看自己的需求選擇合適的滑動方式。實(shí)現(xiàn)滑動的基本原理,有兩個(gè)容器A、B,假如A在外層,B在內(nèi)層;外層的A寬度或高度固定,內(nèi)層容器B寬度或者高度大于A即可實(shí)現(xiàn)滾動。
實(shí)現(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實(shí)現(xiàn)滑動,用ionic自帶的滑動組件實(shí)現(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實(shí)現(xiàn)滾動,使用css的overflow屬性atuo或者scroll實(shí)現(xiàn)滾動
使用css的overflow屬性atuo或者scroll實(shí)現(xiàn)滾動,使用css的overflow屬性atuo或者scroll實(shí)現(xiàn)滾動
使用css的overflow屬性atuo或者scroll實(shí)現(xiàn)滾動,使用css的overflow屬性atuo或者scroll實(shí)現(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是實(shí)現(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;">
{wppm3vysvbp}
</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)聽封裝為一個(gè)指令,方便以后滑動時(shí)候使用。
在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ū){wppm3vysvbp}
</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;">
{wppm3vysvbp}每行
</div>
</div>
</div>
</div>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript版TAB選項(xiàng)卡效果實(shí)例
tab選項(xiàng)卡效果就是當(dāng)我指上或經(jīng)過時(shí)就會顯示這個(gè)選項(xiàng)卡下面的內(nèi)容,下面我來給大家介紹一款純js實(shí)現(xiàn)的TAB選項(xiàng)卡效果,有需要的朋友可以參考一下2013-08-08
promise處理多個(gè)相互依賴的異步請求(實(shí)例講解)
下面小編就為大家?guī)硪黄猵romise處理多個(gè)相互依賴的異步請求(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
JavaScript 實(shí)現(xiàn)自己的安卓手機(jī)自動化工具腳本(推薦)
這篇文章主要介紹了 JavaScript 實(shí)現(xiàn)自己的安卓手機(jī)自動化工具腳本,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
微信小程序?qū)崿F(xiàn)點(diǎn)擊卡片 翻轉(zhuǎn)效果
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)點(diǎn)擊卡片 翻轉(zhuǎn)效果本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
JavaScript開發(fā)人員的10個(gè)關(guān)鍵習(xí)慣小結(jié)
還在一味沒有目的的編寫JavaScript代碼嗎?那么你就OUT了!讓我們一起來看看小編為大家搜羅的JavaScript開發(fā)人員應(yīng)該具備的十大關(guān)鍵習(xí)慣吧2014-12-12

