轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

微信小程序之图片轮播及文件上传

发布者: qjli666717 | 发布时间: 2017-9-10 08:20| 查看数: 2310| 评论数: 1|帖子模式

作者:xixially 
刚刚接触微信小程序,看着网上的资源写了个小例子,本地图片轮播以及图片上传。

图片轮播: 
index.js

  1. var app = getApp()
  2. Page({
  3. data:{
  4. mode: 'aspectFit',
  5. // src:'../images/timg1.jpg',
  6. imgUrls:[
  7. '../images/1.jpg',
  8. '../images/2.jpg',
  9. '../images/3.jpg',
  10. '../images/4.jpg'
  11. ],
  12. indicatorDots: true, //是否出现焦点
  13. autoplay: true, //是否自动播放
  14. interval: 2000, //自动播放时间间隔
  15. duration: 1000, //滑动动画时间
  16. userInfo: {}
  17. },
  18. onLoad:function(){
  19. console.log('onLoad Test');
  20. }
  21. })

注:imgUrls中为本地图片数组。

index.wxml:

  1. <swiper indicator-dots = "{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
  2. <block wx:for="{{imgUrls}}">
  3. <swiper-item class="swiper_i">
  4. <image src="{{item}}" mode="{{mode}}" class="slide-image" />
  5. </swiper-item>
  6. </block>
  7. </swiper>

index.json:

  1. {
  2. "navigationBarTitleText": "相册"
  3. }

index.wxss:

  1. .slide-image{
  2. width:100%;
  3. height:100%;
  4. }
  5. .swiper_i{
  6. text-align: center;
  7. width:100%;
  8. }

好了,这是图片轮播的几个文件。会在app.json中配置。 
接下来是图片上传的。因为没有服务器,暂时不能上传,但是可以从本地相册或拍照获取照片。

upload.js:

  1. var app = getApp()
  2. Page({
  3. data:{
  4. // text:"这是一个页面"
  5. source: '',
  6. tt:false
  7. },
  8. /**
  9. * 选择相册或者相机 配合上传图片接口用
  10. */
  11. onLoad: function() {
  12. var that = this;
  13. wx.chooseImage({
  14. count: 1,
  15. //original原图,compressed压缩图
  16. sizeType: ['original'],
  17. //album来源相册 camera相机
  18. sourceType: ['album', 'camera'],
  19. //成功时会回调
  20. success: function(res) {
  21. //重绘视图
  22. that.setData({
  23. source: res.tempFilePaths,
  24. tt:true
  25. })
  26. /* var tempFilePaths = res.tempFilePaths
  27. wx.uploadFile({
  28. url: 'https://', //仅为示例,非真实的接口地址
  29. filePath: tempFilePaths[0],
  30. name: 'file',
  31. formData:{
  32. 'user': 'test'
  33. },
  34. success: function(res){
  35. var data = res.data
  36. //do something
  37. }
  38. })*/
  39. }
  40. })
  41. },
  42. /*onHide:function(){
  43. this.setData({
  44. source:''
  45. })
  46. }*/
  47. })

upload.json:

  1. {
  2. "navigationBarTitleText": "上传图片"
  3. }

upload.wxml:

  1. <view class="Container">
  2. <image src="{{source}}" mode="aspectFit" class="image-i"/>
  3. <block wx:if="{{tt}}">
  4. <button type="primary" bindtap="listenerButtonChooseImage">确认上传</button>
  5. </block>
  6. </view>

upload.wxss:

  1. /* pages/upload/upload.wxss */
  2. .Container{
  3. text-align:center;
  4. width:100%;
  5. }
  6. .image-i{
  7. width:100%;
  8. height:100%;
  9. }

app.js为空。 
app.json:

  1. {
  2. "pages": [
  3. "pages/index/index",
  4. "pages/upload/upload"
  5. ],
  6. "window": {
  7. "navigationBarTextStyle": "black",
  8. "navigationBarTitleText": "演示2",
  9. "navigationBarBackgroundColor": "#fbf9fe",
  10. "backgroundColor": "#fbf9fe"
  11. },
  12. "networkTimeout": {
  13. "request": 10000,
  14. "connectSocket": 10000,
  15. "uploadFile": 10000,
  16. "downloadFile": 10000
  17. },
  18. "tabBar": {
  19. "list": [{
  20. "pagePath": "pages/index/index",
  21. "text": "显示图片",
  22. "iconPath": "pages/images/icon_API.png",
  23. "selectedIconPath": "pages/images/icon_API_HL.png"
  24. },{
  25. "pagePath": "pages/upload/upload",
  26. "text": "上传",
  27. "iconPath": "pages/images/icon_API.png",
  28. "selectedIconPath": "pages/images/icon_API_HL.png"
  29. }]
  30. },
  31. "debug": true
  32. }

最新评论

疯叽病患者还睡 发表于 2022-4-27 22:46
怎么在源代码里面加入音乐

浏览过的版块

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表