【移动应用开发技术】怎么从零开发微信小程序搜索组件_第1页
【移动应用开发技术】怎么从零开发微信小程序搜索组件_第2页
【移动应用开发技术】怎么从零开发微信小程序搜索组件_第3页
【移动应用开发技术】怎么从零开发微信小程序搜索组件_第4页
【移动应用开发技术】怎么从零开发微信小程序搜索组件_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】怎么从零开发微信小程序搜索组件

这篇文章将为大家详细讲解有关怎么从零开发微信小程序搜索组件,在下觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。如何从零开发微信小程序搜索组件为组件设置一个容器,在容器中放置搜索图标、输入框、清除文字按钮和搜索按钮。<view

class="input-wrapper">

<icon

class="search-icon"/>

<input

placeholder='{{placeholder}}'

value='{{inputValue}}'

bindinput='handleInput'

bindconfirm='handleSearch'

bindfocus='inputFocused'>

</input>

<view

class="close-icon-wrapper">

<icon

class="close-icon"/>

</view>

搜索

</view>组件样式(推荐学习:小程序开发)container:高度100rpx,背景色#eee,flex布局。input-wrapper:高度80rpx,背景色#fff,flex布局,border-radius:20rpx。search-icon:宽高32rpx。input:字体和光标颜色#000,字体大小32rpx。close-icon-wrapper:宽高80rpx,绝对定位。text:搜索按钮宽110rpx,高65rpx,绝对定位,左边框2rpxsolid#eee。container:高度100rpx,背景色#eee,flex布局。input-wrapper:高度80rpx,背景色#fff,flex布局,border-radius:20rpx。search-icon:宽高32rpx。input:字体和光标颜色#000,字体大小32rpx。close-icon-wrapper:宽高80rpx,绝对定位。text:搜索按钮宽110rpx,高65rpx,绝对定位,左边框2rpxsolid#eee。.container

{

background:

#eee;

height:

100rpx;

width:

100%;

display:

flex;

justify-content:

center;

align-items:

center;

}

.input-wrapper

{

display:

flex;

align-items:

center;

height:

80rpx;

width:

80%;

background:

#fff;

border-radius:

20rpx;

}

.input-wrapper

.search-icon

{

margin-left:

20rpx;

width:

32rpx;

height:

32rpx;

}

.input-wrapper

input

{

margin-left:

10rpx;

color:

#000;

font-size:

32rpx;

caret-color:

#000;

width:

60%;

}

.input-wrapper

.close-icon-wrapper{

position:

absolute;

left:

480rpx;

width:

80rpx;

height:

80rpx;

background:#fff;

display:

flex;

justify-content:

center;

align-items:

center;

}

.input-wrapper

.close-icon

{

width:

42rpx;

height:

42rpx;

}

.input-wrapper

text

{

position:

absolute;

right:

80rpx;

width:

110rpx;

height:

65rpx;

padding:

0;

background:

#fff;

display:

flex;

justify-content:

center;

align-items:

center;

font-size:

32rpx;

border-left:

2rpx

solid

#eee;

}组件功能组件的构造器中要注意区分properties和data,properties中写组件的对外属性,data写组件的对内属性。在本搜索组件中placeholder和value从页面传来,所以它们写在properties中,控制清除按钮是否出现的showCloseIcon要写在data中。properties:

{

placeholder:

{

type:

String,

value:

'搜索'

//

如果页面不传placeholder,显示“搜索”

},

inputValue:

{

type:

String

}

},

data:

{

showCloseIcon:

false,

},2、方法设置事件流程(1)光标不聚焦,没有任何输入——显示搜索图标、placeholder和搜索按钮。(2)光标聚焦,没有任何输入——光标闪烁,显示搜索图标、placeholder和搜索按钮。(3)光标聚焦,有输入——光标闪烁,显示搜索图标、输入文字、清除按钮和搜索按钮。(4)光标不聚焦,有输入——显示搜索图标、输入文字、清除按钮和搜索按钮。(5)按回车搜索——清除按钮隐藏。(6)点击搜索按钮——清除按钮隐藏。由此可见,需要input组件的聚焦和键盘输入事件。<view

placeholder='{{placeholder}}'

value='{{inputValue}}'

bindinput='handleInput'

bindconfirm='handleSearch'

bindfocus='inputFocused'>

</view>inputFocused:如果聚焦时,输入框中有内容,显示closeIcon;handleInput:如果输入时没有内容,不显示closeIcon,有内容,显示closeIcon并把值存入value。handleSearch:点击回车后,不显示closeIcon。triggerEvent:自定义组件触发事件时,需要使用triggerEvent方法,指定事件名、detail对象和事件选项。inputFocused:如果聚焦时,输入框中有内容,显示closeIcon;handleInput:如果输入时没有内容,不显示closeIcon,有内容,显示closeIcon并把值存入value。handleSearch:点击回车后,不显示closeIcon。triggerEvent:自定义组件触发事件时,需要使用triggerEvent方法,指定事件名、detail对象和事件选项。inputFocused(e)

{

if

(e.detail.value

!==

'')

{

this.setData({

showCloseIcon:

true,

});

}

},

handleInput(e)

{

if

(e.detail.value

==

'')

{

this.setData({

showCloseIcon:

false,

});

}

else

{

this.setData({

showCloseIcon:

true,

});

this.triggerEvent('handleInput',

{

value:

e.detail.value

});

}

},

handleSearch()

{

//

点击键盘上的回车,调用此方法

this.setData({

showCloseIcon:

false,

});

console.log('handleSearch',

this.data.inputValue);

},搜索分别为closeIcon和搜索按钮添加点击事件。分别为closeIcon和搜索按钮添加点击事件。clearValue()

{

this.triggerEvent('handleInput',

{

value:

''

});

this.setData({

showCloseIcon:

false,

});

},

onTap()

{

this.setData({

showCloseIcon:

false,

});

console.log('onTap',

this.data.inputValue);

},组件

json

{

component:true

}页面json工程的名字是cookbook,这里组件前缀统一为ck。{

usingComponents:{

ck-input:/components/search/index

}

}页面wxml<input

placeholder='搜你想吃的'

inputV

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论