微信小程序可移動浮動客服按鈕

微信小程序可移動浮動客服按鈕

  • 使用技術 Taro
  • 微信小程序 WSX

先看下效果圖(免費的只能截取60幀,所以分3個圖吧)

1.gif
2.gif
3.gif

一開始我是選擇用選擇用setState的方式,在原生就是setData了修改浮動框的坐標,后來發(fā)現(xiàn)太卡了,而且真機操作一點都不流暢,通過一番搜索,最后找到了微信小程序的wsx

直接上代碼吧

// CustomerService.js

import Taro, { Component } from '@tarojs/taro';
import { View, Image, Button } from '@tarojs/components';
import styles from './CustomerService.module.less';
import './CustomerService.less';
class CustomerService extends Component {
    constructor() {
        super(...arguments)
        this.state = {
            active: false,
            startPoint: [0, 300],
            maxHeight: Taro.getSystemInfoSync().windowHeight
        }
    }

    test = (styles) => {
        this.setState({
            active: false
        })
    }

    render() {
        const { startPoint, active, maxHeight } = this.state;
        return (
                <View className={styles.component + " " + "moveable"}    >
                    <wxs module="move" src="./move.wxs"></wxs>
                    <View
                        className={ styles.active + " " + ( active ? '' : " landLeft" ) } 
                        onTouchMove='{{move.touchmove}}' 
                        onTouchEnd='{{move.touchend}}'
                        data-height={maxHeight}
                        onTest={this.test}
                        id="land"
                    >
                        <Button className={styles.btn} hoverClass={"none"}  open-type="contact" >客服</Button>
                    </View>
                </View>
            
        )
    }
}
// 同目錄下創(chuàng)建 move.wxs

function touchmove(event, ins) {
    var touch = event.touches[0] || event.changedTouches[0];
    var x = touch.clientX;
    var y = touch.clientY;

    var landInstance = ins.selectComponent('#land');
    landInstance.removeClass('landRight');
    landInstance.removeClass('landLeft');
    landInstance.addClass('activeRadius');

    var dataSet = landInstance.getDataset();
    var maxHeight = dataSet.height;

    ins.selectComponent('.moveable').setStyle({
        left: x - 30 + "px",
        top: y - 30 + "px"
    })
 
    return false
}

function touchend(event, ins) {
    var touch = event.touches[0] || event.changedTouches[0];
    var x = touch.clientX;
    var y = touch.clientY;
    var isLeft = x < 187.5;
    var addClassName = isLeft ? 'landLeft': 'landRight';
    var landInstance = ins.selectComponent('#land')
    var dataSet = landInstance.getDataset();
    var maxHeight = dataSet.height;
    var style = {
        top: y <= 0 ? 30 + 'px' : y >= maxHeight - 160 ? maxHeight - 160 + "px" :  y - 30 + "px"
    }

    if(isLeft) {
        style["left"] = 0 ;
       
    } else {
        style["right"] = 0;
      
    }
    
    ins.selectComponent('.moveable').setStyle(style)
    landInstance.removeClass("activeRadius");
    landInstance.removeClass(isLeft ? 'landRight' : 'landLeft')
    landInstance.addClass(addClassName)
    ins.callMethod("test", event);
    return false
}

module.exports = {
    touchmove: touchmove,
    touchend: touchend
}

//CustomerService.module.less  局部css,
    .component {
        position: fixed;
        top: 50%;
        z-index: 99;

        .landLeft,
        .landRight,
        .active {
            background-color: rgba(241, 241, 241, 0.75);
            border: 1px solid #d9d9d9;
            padding: 15px 15px;
            
            .btn {
                width: 75px;
                height: 75px;
                background-color: rgb(246, 166, 67);
                border-radius: 50%;
                color: #fff;
                font-size: 26px;
                padding: 0 0;
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: center;
            }
        }     
    }
// CustomerService.module.less 全局css
.landLeft {
    border-top-right-radius: 50%;
    border-bottom-right-radius: 50%;
}
.landRight {
    border-top-left-radius: 50%;
    border-bottom-left-radius: 50%;
}

.activeRadius {
    border-radius: 50%;
    padding: 18px;
}

時間匆忙,暫時記錄著

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容