cu-keyboard.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <view class="key-container" @click="hide" v-show="showMask">
  3. <uni-transition :modeClass="['slide-bottom']" :show="show"
  4. :styles="{height:'100vh'}"
  5. :duration="duration">
  6. <view class="key-content" @click.stop>
  7. <slot></slot>
  8. <view class="key-box block flex">
  9. <view class="key-left">
  10. <view class="key-top flex flex-wrap">
  11. <view class="btn-box" v-for="(item,index) in numArr" :key="index">
  12. <button hover-class="active" class="cu-btn key-btn text-black text-xl" @click.stop="keydown(item)">{{item}}</button>
  13. </view>
  14. </view>
  15. <view class="key-bottom">
  16. <view class="btn-zero">
  17. <button hover-class="active" class="cu-btn key-btn text-black text-xl" @click.stop="keydown('0')">0</button>
  18. </view>
  19. <view class="btn-point">
  20. <button hover-class="active" class="cu-btn key-btn text-black text-xl" @click.stop="keydown('.')">.</button>
  21. </view>
  22. </view>
  23. </view>
  24. <view class="key-right">
  25. <view class="del">
  26. <button hover-class="active" class="cu-btn key-btn text-black text-xl" @click.stop="del">
  27. <text class="zm iconbackspace text-xl"></text>
  28. </button>
  29. </view>
  30. <view class="confirm">
  31. <button hover-class="active" :style="confirmBgColor" class="cu-btn" @click.stop="confirm">
  32. <text class="confirm-text">{{confirmText}}</text>
  33. </button>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </uni-transition>
  39. </view>
  40. </template>
  41. <script>
  42. /**
  43. * 付款组件
  44. * @property {Number} duration - 弹出动画时长,默认为300
  45. * @event {Function} change - 数字改变触发,参数为数字
  46. * @event {Function} confirm - 付款时触发,参数为数字
  47. * @event {Function} hide - 关闭键盘触发,参数为空
  48. */
  49. // 使用方法,查看同级目录exmple
  50. import uniTransition from '../uni-transition/uni-transition.vue'
  51. export default{
  52. components:{
  53. uniTransition
  54. },
  55. props:{
  56. duration:{
  57. type:Number,//弹出动画时常
  58. default:300
  59. },
  60. confirmText:{
  61. type:String,
  62. default:'划扣'
  63. },
  64. confirmStyle:{
  65. type:Object,
  66. default:()=>{
  67. return{
  68. backgroundColor:'#0091fe'
  69. }
  70. }
  71. },
  72. confirmBgColor:{
  73. type:String,
  74. default:'background-color:#0091fe'
  75. }
  76. },
  77. data(){
  78. return{
  79. value:'',//输出的值
  80. show:false,//显示键盘
  81. showMask:false,//遮罩层
  82. numArr:[1,2,3,4,5,6,7,8,9]
  83. }
  84. },
  85. watch:{
  86. value(newval,oldval){
  87. this.$emit("change",newval);
  88. }
  89. },
  90. methods:{
  91. close(){
  92. this.show = false;
  93. setTimeout(()=>{
  94. this.showMask = false;
  95. },this.duration)
  96. },
  97. open(){
  98. this.value = '';
  99. this.show = true;
  100. this.showMask = true;
  101. },
  102. del(){
  103. if(this.value.length){
  104. this.value = this.value.slice(0,this.value.length-1);
  105. }
  106. },
  107. keydown(e){
  108. switch(e){
  109. case '.':
  110. // 当输入为点的时候,如果第一次输入点,则补零
  111. if(!this.value.length){
  112. this.value = '0.';
  113. }else{
  114. if(this.value.indexOf('.')>-1){
  115. // 如果已经有点,则无效
  116. }else{
  117. this.value = this.value+''+e;
  118. }
  119. }
  120. break;
  121. case '0':
  122. if(this.value.length === 0){
  123. this.value = this.value+''+e;
  124. }
  125. if(Number(this.value) === 0 && this.value.indexOf('.')== -1){
  126. // 当输入为零的时候,如果value转换成数字为零,且没有点则无效
  127. }else{
  128. this.value = this.value+''+e;
  129. }
  130. break;
  131. default:
  132. this.value = this.value+''+e;
  133. break;
  134. }
  135. },
  136. hide(){
  137. this.$emit('hide');
  138. this.close();
  139. },
  140. confirm(){
  141. this.$emit('confirm',this.value);
  142. // this.close();
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss" scoped>
  148. @font-face {
  149. font-family: 'zm'; /* project id 2442084 */
  150. src: url('https://at.alicdn.com/t/font_2442084_o72ps3802ih.eot');
  151. src: url('https://at.alicdn.com/t/font_2442084_o72ps3802ih.eot?#iefix') format('embedded-opentype'),
  152. url('https://at.alicdn.com/t/font_2442084_o72ps3802ih.woff2') format('woff2'),
  153. url('https://at.alicdn.com/t/font_2442084_o72ps3802ih.woff') format('woff'),
  154. url('https://at.alicdn.com/t/font_2442084_o72ps3802ih.ttf') format('truetype'),
  155. url('https://at.alicdn.com/t/font_2442084_o72ps3802ih.svg#zm') format('svg');
  156. }
  157. .zm {
  158. font-family: "zm" !important;
  159. font-size: 16px;
  160. font-style: normal;
  161. -webkit-font-smoothing: antialiased;
  162. -moz-osx-font-smoothing: grayscale;
  163. }
  164. .iconbackspace:before {
  165. content: "\ef11";
  166. }
  167. .flex{
  168. display: flex;
  169. }
  170. .flex-wrap{
  171. flex-wrap: wrap;
  172. }
  173. .cu-btn {
  174. position: relative;
  175. border: 0rpx;
  176. display: inline-flex;
  177. align-items: center;
  178. justify-content: center;
  179. box-sizing: border-box;
  180. padding: 0 30rpx !important;
  181. font-size: 28rpx !important;
  182. height: 64rpx !important;
  183. line-height: 1 !important;
  184. text-align: center;
  185. text-decoration: none;
  186. overflow: visible;
  187. margin-left: initial;
  188. transform: translate(0rpx, 0rpx) !important;
  189. margin-right: initial;
  190. }
  191. .cu-btn::after {
  192. display: none;
  193. }
  194. .text-xl{
  195. font-size:36rpx !important;
  196. font-weight: bold !important;
  197. font-family: 'microsoft-yahei';
  198. }
  199. .text-black{
  200. color:#333;
  201. }
  202. .active{
  203. background-color: #ddd !important;
  204. transform: translate(2rpx,2rpx);
  205. }
  206. .key-container{
  207. position: fixed;
  208. bottom: 0;
  209. top:0;
  210. left:0;
  211. right:0;
  212. .key-content{
  213. position: absolute;
  214. bottom: 0;
  215. width: 100vw;
  216. background-color: #F7F7F7;
  217. }
  218. }
  219. .key-box{
  220. width: 100%;
  221. box-sizing: border-box;
  222. padding:10rpx 10rpx 0;
  223. .key-left{
  224. width: 75%;
  225. }
  226. .key-right{
  227. width: 25%;
  228. display: flex;
  229. flex-direction: column;
  230. }
  231. .key-bottom{
  232. width: 100%;
  233. display: flex;
  234. }
  235. }
  236. .del{
  237. width: 100%;
  238. }
  239. .btn-box{
  240. width: 33.33%;
  241. padding:0 10rpx 10rpx 0;
  242. box-sizing: border-box;
  243. }
  244. .btn-zero{
  245. width: 66.66%;
  246. padding:0 10rpx 10rpx 0;
  247. box-sizing: border-box;
  248. }
  249. .btn-point{
  250. width: 33.33%;
  251. padding:0 10rpx 10rpx 0;
  252. box-sizing: border-box;
  253. }
  254. .key-right{
  255. flex-shrink: 0;
  256. }
  257. .key-btn{
  258. background-color: #fff;
  259. width: 100% !important;
  260. height: 90rpx !important;
  261. }
  262. .confirm{
  263. width: 100%;
  264. flex:1;
  265. padding: 10rpx 0 10rpx 0;
  266. box-sizing: border-box;
  267. button{
  268. width: 100% !important;
  269. height: 100% !important;
  270. .confirm-text{
  271. color:#fff;
  272. display: block;
  273. font-size: 32rpx;
  274. }
  275. }
  276. }
  277. </style>