小程序滑动点击切换中间大图两边小图

小程序 滑动点击 切换中间大图 两边小图

代码太老了已不建议阅读, 简单看下思路即可

整体思路, 使用小程序API的animation动画和组件的canvas中的bindtouchstart.bindtouchmove和bindtouchend组合

移动的效果和轮播图原理相同 ,根据触摸距离判定点击和滑动
<view class='middle'>
    <view class='middle-down'> 
        <image class='middle-phone' style='width:{{middlePhoneWidth}}px;margin-left: -    {{middlePhoneWidthMarLeft }}px;' src='' > </image> 
    </view> 

    <view class='swiper'> 
        <view class='swiper-lb' bindtouchmove="scroll" bindtouchstart='startTou' bindtouchend='endTou'> 
        <view animation="{{animationData}}" class='swiper-ul' style='width:{{swiperUlWidth}}px;left: {{swiperLeft}}px;'> 
        <view id='swiper{{index}}' class='swiper-li' wx:for="{{images}}" style='width:{{swiperLiWidth}}px;'> 
        <image animation="{{styleArr[index].animationliscal}}" class='swiper-image swiper-image{{index}} ' src='{{item.picUrl}}' style='width:{{styleArr[index].imgwidth}}%;height:{{styleArr[index].imgheight}}%'></image> 
    </view>
</view>
.middle{ width: 100%; height: 540rpx; position: relative; } 
.middle-down{ width: 100%; height: 100%; position: relative; } 
.middle-bg{ width: 100%; height: 100%; position: absolute; left: 0; right: 0; top: 0; bottom: 0; } 
.middle-phone{ width: 282rpx; height: 383rpx; position: absolute; left: 50%; top: -20rpx; z-index:10; } 
.swiper{ position: absolute; width: 100%; left: 0; top: 14px; z-index: 11; height: 205px; } .swiper-lb{ height: 205px; width: 100%; /* background: #fff; */ overflow: hidden; position: relative; } 
.swiper-ul{ position: absolute; top: 0; left:0; height: 152px; /* background: #ffe400; */ } .swiper-li{ height: 100%; padding-left: 5px; padding-bottom: 10px; float: left; box-sizing: border-box; display: flex; justify-content: center; align-items: center; } 
.swiper-image{ } 
.swiper-image1{ width: 100%; height: 100%; }

根据小程序API的animation动画和组件的canvas中的bindtouchstart.bindtouchmove和bindtouchend组合

imageWidth:0, 
imageHeight:0, 
phoneWidth: 0, //屏幕宽 根据屏幕的宽度,三分之一为li的宽度 
phoneHeight: 0, //屏幕高 
swiperWidth:0, 
imgindex:1,//中间的下标 重点 
middlePhoneWidthMarLeft:0, //背景的图片的margin-left=-aaa 
middlePhoneWidth:0, //背景 
swiperUlWidth:0, //移动的ul的宽度 
swiperLiWidth:0, //移动的li的宽度 
swiperLeft:0, //移动的定位
left animationData: {},//运动 
startClientX: 0,//点击开始 X 轴位置 
endClientX: 0,//点击结束 X 轴位置 
images:[], //图片的数据 
styleArr:[], //所有图片的样式数组 对中间的图片放大的操作组 
duration: 1000, //动画时间

然后是初始对数据的第二张图进行放大处理

首先是初始化 获取数据 
onLoad: function () { 
    console.log('onLoad') 
    var _this = this; 
    //===取屏幕宽度======= 
    wx.getSystemInfo({ 
        success: function (res) { 
            // _this.data.screenHeight= res.windowHeight; 
            _this.setData({ phoneWidth: res.windowWidth }) 
        } 
    });
});
//=======带data参数 请求数据====================
wx.getStorage({
   key: 'Id',
   success: function (res) {
       console.log(res.data)
       wx.request({
           url: '',
           data: {
                 Id: res.data,
           },
           method: "POST",
           header: {
                 'content-type': 'application/x-www-form-urlencoded' // 默认值
           },
           success: function (res) {
                console.log(res.data)
                if (res.data.respCode == '00000') {
                  _this.setData({
                     images: res.data.respData.pics,
                     //图片数据json的示例
                      persNub: res.data.respData.count,
                   })
                   console.log(_this.data.images)
                   //swiper li 赋值 宽度
                   let swiperLiWidth = _this.data.swiperLiWidth;//li宽
                   let phoneWidth = _this.data.phoneWidth; //屏幕宽
                   swiperLiWidth = phoneWidth / 3;   //li的宽度赋值 三分之一的屏幕宽度
                   var arrimages = _this.data.images;//获取图片Arr的数组
                   let swiperUlWidth = _this.data.swiperUlWidth; //移动的ul 的宽度
                   swiperUlWidth = swiperLiWidth * arrimages.length  //赋值移动的ul 的宽度
                   let middlePhoneWidth = swiperLiWidth + 30; // 背景参照物  可不写
                   let middlePhoneWidthMarLeft = middlePhoneWidth / 2; 背景参照物 可不写
                   //初始化所有的图片的宽度占70%父级宽高
                   var arrimages = _this.data.images; 
                   let styleArr = _this.data.styleArr; 
                   for (let i = 0; i < arrimages.length; i++) {
                        var obj = {
                            imgwidth: 70,
                            imgheight: 70,
                            animationliscal: ""
                         }
                         styleArr.push(obj)
                   }
                   //对中间图赋值初始化100%宽高
                   styleArr[1] = {
                        imgwidth: 100,
                        imgheight: 100,
                        animationliscal: ""
                   };
                   // 传输data 赋值  这样写不知道对不对
                   _this.setData({
                        styleArr: styleArr,
                        swiperUlWidth: swiperUlWidth,
                        swiperLiWidth: swiperLiWidth,
                        middlePhoneWidth: swiperLiWidth + 30,
                        middlePhoneWidthMarLeft: middlePhoneWidthMarLeft
                    })
                    } else {
                       console.log(res.data.respMsg)
                    }
                    },
                    fail: function (res) {
                        console.log(res)
                    }
                })
            }
        })
})

进行滑动点击的

滑动为0时,就为点击 ;不为0  根据结束位置-初始位置来设置相应的移动  
具体请参考 小程序开发 组建中的canvas 滑动点击的事件.至于为什么在canvas中,可能是小程序开发者写错位置了,不需要写canvas

startTou:function(e){ 
    let _this = this; 
    _this.data.startClientX = e.touches[0].clientX; 
    //触摸按下 距离屏幕左边的值 }, 
scroll: function (e) { 
    let _this = this; 
    _this.data.endClientX = e.touches[0].clientX; //滑动值 
}, 
endTou: function (e) { 
    let _this = this;
	// API animation 滑动动画创建
    var animation = wx.createAnimation({
        duration: 1000,
        timingFunction: 'ease',
    })
    var swiperLiWidthLeft= _this.data.swiperLiWidth;
    this.animation = animation;
    let startClientX = _this.data.startClientX;
    let endClientX = _this.data.endClientX;
    let phoneWidth = _this.data.phoneWidth;
    if ( endClientX ==0 ) {   //move的值为0 时定为点击  
            //点击的时候 点左边,左边的小图,移动到中间变大 点击右边的时候 同理
            if (startClientX < phoneWidth / 2-70) {  
                //点击开始的位置,与图片的一半减70px  为左边点击
                this.animation = animation;
                animation.left(_this.data.swiperLeft).step() //移动动画
                let imgindex = _this.data.imgindex -1; //下标值
                if (imgindex < 0 ){
                    console.log("超出了最小数组长度")
                    return;
                }
                _this.setData({
                    swiperLeft: _this.data.swiperLeft + swiperLiWidthLeft,  //ul向右移动值

                    imgindex: _this.data.imgindex - 1, //下标值
                    animationData: animation.export()
                })
                console.log("左边点击"  + _this.data.imgindex)

            }else if (startClientX > phoneWidth / 2+70) {   
                //点击开始的位置,与图片的一半减70px  为右边点击

                let imgindex = _this.data.imgindex +1;
                if (imgindex > _this.data.images.length - 1 ) {
                    console.log("超出了数组最大长度")
                    return;
                }
                console.log("右边点击"  + _this.data.imgindex)
                animation.left(_this.data.swiperLeft).step()  //移动动画
                _this.setData({
                    swiperLeft: _this.data.swiperLeft - swiperLiWidthLeft,//UL向左移动
                    imgindex: _this.data.imgindex +1, //下标的值
                    animationData: animation.export()
                })
            }else{   //点击中间的大图,带参跳入图片的详情
                let imgindexclick = _this.data.imgindex;
                let picUrl = _this.data.images[imgindexclick].picUrl;
                let clicks = _this.data.images[imgindexclick].clicks;
                let picUpTime = _this.data.images[imgindexclick].picUpTime;
                let picId = _this.data.images[imgindexclick].picId;
                wx.navigateTo({
                    url: './../PictDetails/PictDetails?picUrl=' + picUrl 
                })
            }
        }else{  
            //滑动左边 ul向左移动 右边的小图放大  滑动右边ul向右移动 右边的小图放大
            if (endClientX -startClientX   > 0) {  
                let imgindex = _this.data.imgindex -1;
                if (imgindex <0 ) {
                    console.log("超出了")
                    return;
                }
                animation.left(_this.data.swiperLeft).step() 
                //移动动画
                _this.setData({
                    swiperLeft: _this.data.swiperLeft + swiperLiWidthLeft, 
                    //右边滑动 ul向右移动
                    imgindex: _this.data.imgindex - 1,
                    animationData: animation.export()
                })
                console.log("右边滑动" + _this.data.imgindex)

            }
            if (endClientX - startClientX  < 0) {
                let imgindex = _this.data.imgindex + 1;
                if (imgindex > _this.data.images.length - 1) {
                    console.log("超出了")
                    return;
                }

                this.animation = animation

                animation.left(_this.data.swiperLeft).step() 
                //移动动画

                _this.setData({
                    swiperLeft: _this.data.swiperLeft - swiperLiWidthLeft,  
                    //左边滑动 ul向左移动

                    imgindex: _this.data.imgindex + 1, //下标的值
                    animationData: animation.export()
                })
                console.log("左边滑动" + _this.data.imgindex)
            }

        }

//遍历数组 与下标值相等的样式数组,即为中间的大图的下标

修改这个下标的样式,就可以放大, 其他的重置

for (let a = 0; a < NewstyleArr.length;a++ ){ 
    if (a == _this.data.imgindex ){
        NewstyleArr[_this.data.imgindex] = { 
            imgwidth: 100, 
            imgheight: 100, 
            animationliscal: "" 
        }; 
    }else{
        NewstyleArr[a] = { 
            imgwidth: 70, 
            imgheight: 70, 
            animationliscal: "" 
        }; 
    } 
}

//返回修改的数据数组到data中
_this.setData({
      startClientX : 0 ,
      endClientX: 0 ,
      styleArr: NewstyleArr 
})

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
### 回答1: UGUI是Unity引擎中的一套用户界面系统,可以用于开发游戏中的各种UI界面。在UGUI中,可以通过设置滑动条的大小和位置来实现中间两边小的滑动效果。 首先,我们可以通过在Scene场景中创建一个空的Canvas对象来添加UI元素。然后在Canvas上创建一个Image对象,作为滑动条的背景。可以通过调整Image对象的大小和位置,设置为中间两边小的形状。 接下来,在Canvas上创建一个Slider对象,并设置该Slider的RectTransform属性,使其与Image对象的大小和位置保持一致。在Slider对象上添加Slider组件,并调整Value属性的值,使Slider的拇指(Thumb)控制器位于中间位置。 然后,通过调整Image对象的渐变效果,使得滑动条在两边逐渐变小。可以使用渐变图像或Gradient组件来实现这个效果。将透明度或颜色的渐变应用到Image对象上,使得滑动条的两端逐渐变得不可见或透明。 最后,我们还可以通过添加其他UI元素来增强滑动条的效果,比如在滑动条的两端添加箭头、文本或其他图标。可以使用UGUI内置的Button、Text或Image组件来实现这些效果。 通过以上步骤,我们可以使用UGUI实现中间两边小的滑动效果。调整滑动条和滑动条背景的大小、位置,设置渐变效果和添加其他UI元素,可以使滑动条呈现出符合需求的效果。UGUI提供了简单易用的UI开发工具,为游戏开发者提供了更多自定义UI界面的可能性。 ### 回答2: UGUI(Unity Graphic User Interface)是Unity引擎中用于创建用户界面的工具。对于“中间两边小的滑动”效果,在UGUI中可以通过使用Scroll Rect组件和Content组件来实现。 首先,创建一个UI画布(Canvas),然后在画布上创建一个Panel,并将Panel设置为可滚动的区域。接下来,添加一个Scroll Rect组件到Panel上。在Scroll Rect组件中,将Content属性设置为一个包含滑动内容的RectTransform。 通过调整Content的RectTransform,可以实现“中间两边小”的效果。可以将Content的宽度设置为大于画布宽度的值,然后将锚点和位置调整到正确的位置,使其在Panel中居中显示。根据实际情况,还可以通过添加ContentSize Fitter组件来自动调整Content的大小,以适配内容。 最后,在Scroll Rect组件中,将Horizontal属性设置为true,允许水平滑动。可以调整滑动的灵敏度、惯性、以及滚动条的显示与否,以便实现所需的效果。 通过以上步骤,就可以在UGUI中实现“中间两边小”的滑动效果。可以通过动态改变Content的大小和位置,使内容在中间放大显示,两侧内容缩小显示,并通过滑动实现内容的切换和浏览。这样可以为用户提供更好的交互体验和视觉效果。 ### 回答3: ugui是Unity的一个UI系统,提供了许多用于创建用户界面的工具和组件。其中,中间两边小的滑动效果可以通过ugui中的ScrollRect组件实现。 ScrollRect组件是一个可滚动区域的容器,可以将内容放置在其中,并通过滑动来查看隐藏的内容。想要实现中间两边小的滑动效果,可以通过以下步骤进行设置: 1.在Unity编辑器中,选择需要实现滑动效果的Canvas对象,然后在Inspector面板中点击Add Component按钮,搜索并添加ScrollRect组件。 2.在ScrollRect组件的Inspector面板中,可以设置一些滑动效果的属性。首先,将Horizontal属性和Vertical属性的值都设置为false,这样就只能在一个方向上滑动。 3.然后,在Content属性中,将需要放置在滚动区域内的内容拖拽到这个属性中。例如,可以将一张图片作为背景拖拽到Content属性中。 4.接下来,可以在HorizontalScrollbar和VerticalScrollbar属性中,将Scrollbar预制体拖拽到对应的属性中,用于显示水平和垂直方向上的滑动条。 5.最后,在Viewport属性中,可以设置背景图的大小,将背景图的宽度设置为较大的值,高度设置为较小的值,以实现中间两边小的效果。 设置完上述属性后,运行程序,就可以通过鼠标滑动或触摸操作来查看滚动区域中的内容了。滑动的时候,内容会根据滑动方向进行滚动,实现了中间两边小的效果。 通过ugui中的ScrollRect组件,可以方便地实现中间两边小的滑动效果,适用于开发游戏、应用等需要滚动显示内容的界面设计。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ob杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值