Home >WeChat Applet >Mini Program Development >Method code for implementing the sliding effect of the sidebar in WeChat applet development
Sidebar sliding is a very common function in the development of mobile applications. Of course, there is no exception in small programs. However, not long after the small programs came out, many special effects have not yet mature cases and can only be rewritten natively. So today We have collected four very beautiful sidebar special effects for everyone on the Internet~~
NO1. The sliding effect of the sidebar is as follows:
where w# The code of ##xml is as follows:
<!--page/one/index.wxml--> <view class="page"> <view class="page-bottom"> <view class="page-content"> <view class="wc"> <text>第一个item1</text> </view> <view class="wc"> <text>第二个item2</text> </view> <view class="wc"> <text>第三个item3</text> </view> <view class="wc"> <text>第四个item4</text> </view> </view> </view> <view class="page-top {{open ? 'c-state1' : ''}}"> <image bindtap="tap_ch" src="../../images/btn.png"></image> </view> </view>Build an upper and lower two-layer interfaceWrite a css3 right shift
AnimationStyle.c-state1
.c-state1{ transform: rotate(0deg) scale(1) translate(75%,0%); -webkit-transform: rotate(0deg) scale(1) translate(75%,0%); }Click the
button to add style.c-state1
Click again to remove style.c-state1.c-state2{ transform: rotate(0deg) scale(.8) translate(75%,0%); -webkit-transform: rotate(0deg) scale(.8) translate(75%,0%); }The wxml code is the same as the special effect oneThe only difference between .c-state2 and .c-state1 is the scale value
<font face="""><font style="font-size:15px">Page({ data:{ open : false }, tap_ch: function(e){ if(this.data.open){ this.setData({ open : false }); }else{ this.setData({ open : true }); } } }) </font></font>The code is very simple, it is to control the view’s selection of classes through the open value
tap_start:function(e){ // touchstart事件 this.data.mark = this.data.newmark = e.touches[0].pageX; }, tap_drag: function(e){ // touchmove事件 /* * 手指从左向右移动 * @newmark是指移动的最新点的x轴坐标 , @mark是指原点x轴坐标 */ this.data.newmark = e.touches[0].pageX; if(this.data.mark < this.data.newmark){ this.istoright = true; } /* * 手指从右向左移动 * @newmark是指移动的最新点的x轴坐标 , @mark是指原点x轴坐标 */ if(this.data.mark > this.data.newmark){ this.istoright = false; } this.data.mark = this.data.newmark; }, tap_end: function(e){ // touchend事件 this.data.mark = 0; this.data.newmark = 0; if(this.istoright){ this.setData({ open : true }); }else{ this.setData({ open : false }); } }Judge in tap_drag
Gesture is from left to right, or from right to left
tap_end means the gesture is raised. If it is from left to right, it triggers a sliding from left to right tap_end indicates that the gesture is raised. If it is from right to left, a sliding from right to left is triggered NO4. The sliding effect of the sidebar is as follows:This special effect will slide with the sliding gesture; if it is less than 20% of the screen width when you let go, it will automatically restore; if it exceeds 20% of the screen width when you let go, it will slide to the right~~This The effect is very complex, we split it into multiple steps to analyze~1) The screen moves with the gesture. The JS code is
this.setData({ translate: 'transform: translateX('+(this.data.newmark - this.data.startmark)+'px)' })This The sentence is the key, and it is easy to understand. It is to use js to control the value of translateX on the light blue screen, so that the gesture keeps sliding left and right, and the screen slowly slides along with the gesture. 2) Bounce effectDrag the screen less than 20% of the screen width and restore the default
state; if it exceeds 20%, slide to the far right~~
JS code:if(x < 20%){ this.setData({ translate: 'transform: translateX(0px)' }) }else{ this.setData({ translate: 'transform: translateX('+this.data.windowWidth*0.75+'px)' }) }If it is less than 20%, let translateX(0px) restore the screen; if it is greater than 20%, let tanslateX(75%) move the screen to the right to 75% of the screen.
The above is the detailed content of Method code for implementing the sliding effect of the sidebar in WeChat applet development. For more information, please follow other related articles on the PHP Chinese website!