日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

手机版html页面左右滑动切换页面,移动端手指左右滑动切换内容demo

發(fā)布時間:2025/4/16 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 手机版html页面左右滑动切换页面,移动端手指左右滑动切换内容demo 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

說在開頭

最近移動端做了一個手指左右滑動切換內容的效果demo;

為了表示我的無私,決定分享給諸位;(詳細代碼見附件)

正文

先上html代碼html>

穿衣助理

完成

整個頁面ul部分是需要切換的部分具體內容有js拼接而成

css代碼如下:(這里直接貼上了less編譯之后)body,div,ul{margin:?0px;padding:?0px;}

.m-shape{box-sizing:?border-box;}

.m-shape?.cont{??overflow:?hidden;box-sizing:?border-box;

}

.j-cont{??margin:?0?auto;

width:?100%;}

.m-shape?.cont?ul?{

width:?1000%;

position:?relative;

margin:?0?7%;

overflow:?hidden;

}

.m-shape?.cont?ul?li?{

display:?inline-block;

float:?left;

width:?8%;

padding:?0?0.3%;

overflow:?hidden;

box-sizing:?content-box;

}

.m-shape?.cont?ul?li?.tishi?{

position:?absolute;

border-radius:?50%;

background:?url(../p_w_picpaths/Assist_icon.png)?no-repeat;

background-size:?30px?30px;

width:?30px;

height:?30px;

right:?10px;

top:?10px;

}

.m-shape?.cont?ul?li?.title?{

height:?40px;

line-height:?40px;

text-align:?center;

}

.m-shape?.cont?ul?li?.cont?{

height:?77%;

text-align:?center;

}

.m-shape?.cont?ul?li?.cont?.img?{

height:?100%;

text-align:?center;

}

.m-shape?.cont?ul?li?.cont?.img?img?{

height:?100%;

min-height:?100%;

max-height:?100%;

}

.m-shape?.cont?ul?li?.cont?p?{

text-align:?center;

line-height:?40px;

}

.m-shape?.cont?ul?li?.msg?{

position:?absolute;

top:?100%;

left:?10%;

background:?rgba(0,?0,?0,?0.5);

color:?#fff;

line-height:?30px;

padding:?10px;

width:?80%;

border-radius:?4px;

}

.m-shape?.cont?ul?li?.j-conts_item?{

background:?#9DE0FF;

border-radius:?6px;

position:?relative;

}

.m-shape?.but_cont?{

text-align:?center;

padding:?20px?0;

}

.m-shape?.but_cont?.but?{

background:?#e9494b;

display:?inline-block;

height:?30px;

line-height:?30px;

width:?30%;

border-radius:?15px;

color:?#fff;

}

.title{

text-align:?center;

height:?40px;

line-height:?40px;

}

上面代碼有一個地方要說明一下:

j-cont的大小為保證自適應其大小與手機屏幕一致(通過js實現(xiàn),詳情見后面js)

而后ul的width設置為1000%;即屏幕寬度的10倍;

li的關鍵css如下:width:?8%;

padding:?0?0.3%;

overflow:?hidden;

box-sizing:?content-box;

所以其padding+width = 86%的j-cont,即屏幕寬度的86%;

在執(zhí)行滾動時我也是詞義移動86%;但是存在一問題如下:

第一張圖片左邊沒有圖片;但是必須預留這個位置,不然第一張位置這樣的,后面切換也會有錯位:

所以給ul設置marin:0% 7%;保證首次位置正確,后面每次切換位置也正確。

為了保證所以尺寸的智能設備自由伸縮,寫了一個方法初始化容器的大小://初始化容器

var?html_cont_initialization?=?function?()?{

//初始化容器

$(".j-cont").css({

"height":?$(window).height()?+?"px",

"min-height":?$(window).height()?+?"px"

});

//初始化內容容器

$(".j-conts_item").css({

"height":?$(window).height()?-?110?+?"px",

"min-height":?$(window).height()?-?110?+?"px",

"max-height":?$(window).height()?-?110?+?"px"

});

}

其中110px為頭部title,以及按鈕所在行即:$(".title"),$(".but_cont")高度之和。

還有一段代碼,用來load內容模板(這個地方,現(xiàn)在是假數(shù)據(jù))var?sex_initialization?=?function?()?{

var?html?=?"";

for?(var?i?=?0;?i?

html?+=?'

\\您的體型是?'+?i?+?'\\\

A型

\\';

}

$(".m-shape?ul").append(html);

html_cont_initialization();

}

滑動代碼如下://觸屏左右切換體型效果

function?switchShape()?{

var?startX,?startY,?endX,?endY;

var?isMove?=?false;//觸摸:start,end操作之間是否有move

var?isSwitching?=?false;//是否正在執(zhí)行動畫

var?curIndex?=?0;

var?MaxLi?=?$(".m-shape?ul?li").length?-?1;

$("body").on("touchmove",?".m-shape?ul",?touchMoveHandler);

$("body").on("touchstart",?".m-shape?ul",?touchStartHandler);

$("body").on("touchend",?".m-shape?ul",?touchEndHandler);

//觸屏左右切換體型效果

function?touchStartHandler(event)?{

event.preventDefault();

var?touch?=?event.touches[0];

startY?=?touch.pageY;

startX?=?touch.pageX;

}

function?touchMoveHandler(event)?{

event.preventDefault();

var?touch?=?event.touches[0];

endX?=?touch.pageX;

isMove?=?true;

}

function?touchEndHandler(event)?{

event.preventDefault();

if?(!isMove?||?isSwitching)?{

return;

}

var?w?=?86;

var?curLeft?=?curIndex???-curIndex?*?w?:?0;

var?dirX?=?1;//滑動方向

if?(Math.abs(startX?-?endX)?>?50)?{//滑動間距大于50

if?(startX?-?endX?>?0)?{

if?(curIndex?===?MaxLi)?{//當前是最后一個

return;

}

curIndex++;

}?else?{

if?(0?===?curIndex)?{//當前是第一個

return;

}

dirX?=?-1;

curIndex--;

}

}

moveTo($(this),?"left",?curLeft,?-curIndex?*?w,?43,?dirX);

isMove?=?false;

}

//動畫函數(shù)

//params:對象,css屬性,開始,結束,50ms移動距離,方向1←,-1右

function?moveTo($obj,?objProp,?start,?end,?spacing,?direction)?{

var?temp?=?start;

isSwitching?=?true;

var?moveTimer?=?setInterval(function?()?{

if?((1?===?direction?&&?temp?-?end?<=?0)?||?(-1?===?direction?&&?temp?-?end?>=?0))?{

clearInterval(moveTimer);

isSwitching?=?false;

return;

}

temp?=?temp?-?spacing?*?direction;

$obj.css(objProp,?temp?+?"%");

},?200);

}

}

switchShape();

上面代碼有3點需要注意:每次滑動應包括三個動作touch start,move,end缺一不可;因為觸屏點擊也會觸發(fā)start,end;

新增isMove狀態(tài),每次move為true;end后為false;保證三個動作都觸發(fā)才執(zhí)行滑動。

具體滑動的距離,一般來講30-50直接都可以;

如果當前正在執(zhí)行動畫,那么在此滑動時,其實應該忽略;即滑動動畫執(zhí)行完畢后,再執(zhí)行下一次。

說在最后

本人移動端玩的少,所以考慮難免不周,多多指教!

總結

以上是生活随笔為你收集整理的手机版html页面左右滑动切换页面,移动端手指左右滑动切换内容demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。