小言_互联网的博客

小程序中的轮播图

602人阅读  评论(0)

目录

  • 小程序的宿主环境 - 组件
    • 1.scroll-view 组件的基本使用
    • 2.swiper 和 swiper-item 组件的基本使用
    • 3.text 组件的基本使用
    • 4.rich-text 组件的基本使用
  • 附:微信小程序轮播图单独添加图片、修改轮播图图片、单独修改某张图片
  • 总结

小程序的宿主环境 - 组件

1.scroll-view 组件的基本使用

实现如图的纵向滚动效果


  
  1. < scroll - view class ="container_2" scroll -y >
  2. < view >T < / view >
  3. < view >S < / view >
  4. < view >J < / view >
  5. < / scroll - view >

复制代码


  
  1. .container_2 view{
  2. width: 100px;
  3. height: 100px;
  4. text-align: center;
  5. line-height: 100px;
  6. }
  7. .container_2 view :nth-child( 1){
  8. background-color: red;
  9. }
  10. .container_2 view :nth-child( 2){
  11. background-color: yellowgreen;
  12. }
  13. .container_2 view :nth-child( 3){
  14. background-color: blue;
  15. }
  16. .container_2{
  17. display: flex;
  18. justify-content: space-around
  19. }
  20. .container_2{
  21. border: 1px solid yellowgreen;
  22. height: 130px;
  23. width: 100px;
  24. }

复制代码

scroll-y 改成 scroll-x

实现如图的横向滚动效果:


  
  1. < scroll - view class ="container_2" scroll -x >
  2. < view >横 向 滑 动 演 示 < / view >
  3. < / scroll - view >

复制代码


  
  1. .container_2 view{
  2. width: 300px;
  3. height: 100px;
  4. text-align: center;
  5. line-height: 100px;
  6. }
  7. .container_2 view :nth-child( 1){
  8. background-color: red;
  9. }
  10. .container_2{
  11. display: flex;
  12. justify-content: space-around
  13. }
  14. .container_2{
  15. border: 1px solid yellowgreen;
  16. height: 100px;
  17. width: 100px;
  18. }

复制代码

2.swiper 和 swiper-item 组件的基本使用

实现如图的轮播图效果:


  
  1. <swiper class="container_3" indicator-dots>
  2. <swiper-item>
  3. <view class="item">1 </view>
  4. </swiper-item>
  5. <swiper-item>
  6. <view class="item">2 </view>
  7. </swiper-item>
  8. <swiper-item>
  9. <view class="item">3 </view>
  10. </swiper-item>
  11. <swiper-item>
  12. <view class="item">4 </view>
  13. </swiper-item>
  14. </swiper>

复制代码


  
  1. .container_3{
  2. height: 160px;
  3. }
  4. .item{
  5. height: 100%;
  6. line-height: 150px;
  7. text-align: center;
  8. }
  9. swiper-item :nth-child( 1) .item{
  10. background-color: burlywood;
  11. }
  12. swiper-item :nth-child( 2) .item{
  13. background-color: yellow;
  14. }
  15. swiper-item :nth-child( 3) .item{
  16. background-color: pink;
  17. }
  18. swiper-item :nth-child( 4) .item{
  19. background-color: aqua;
  20. }

复制代码

.swiper 组件的常用属性


属性

类型

默认值

说明

indicator-dots
boolean false 是否显示面板指示点
indicator-color color rgba(0, 0, 0, .3) 指示点颜色
indicator-active-color color #000000 当前选中的指示点颜色
autoplay boolean false 是否自动切换
interval number 5000 自动切换时间间隔
circular boolean false 是否采用衔接滑动

3.text 组件的基本使用

文本组件

类似于 HTML 中的 span 标签,是一个行内元素

通过 text 组件的 selectable 属性,实现长按选中文本内容的效果:


  
  1. <view>
  2. 手机号:
  3. <text selectable>17608777 </text>
  4. </view>

复制代码

4.rich-text 组件的基本使用

富文本组件 支持把 HTML 字符串渲染为 WXML 结构

<rich-text nodes="<h1 style='color:pink;'>一级标题 <h1>"></rich-text>

复制代码

附:微信小程序轮播图单独添加图片、修改轮播图图片、单独修改某张图片


  
  1. <!--pages/swiper/swiper.wxml-->
  2. <text>pages/swiper/swiper.wxml </text>
  3. <!-- 滑块视图 先添加一个滑块容器 -->
  4. <!-- 是否自动播放 ,增加提示点 ,是否衔接滑动(例如从最后一张到第一张),提示点颜色 -->
  5. <swiper
  6. autoplay= " {{false}} "
  7. indicator-dots
  8. circular
  9. indicator-color= "rgba(0,0,0,1)">
  10. <!-- 添加一个内容 更改轮播图图片 -->
  11. <block wx:for=" {{image}} " wx:key="this" wx:for-index="ind1">
  12. <!-- 将该for的下标Index命名为ind1 可以不用block,可以直接在swiper-item使用wx:for-->
  13. <swiper-item >
  14. <image src=" {{item}} " data-ccc="ind1" ></image>
  15. <!-- 将下标给到本地数据库data,并且命名ccc -->
  16. </swiper-item >
  17. </block>
  18. </swiper>
  19. <button bindtap="getImg">更改轮播图的图片 </button>
  20. <button bindtap="getc">在轮播图最后面添加一个图片 </button>
  21. <!-- 单独换图片 -->
  22. <swiper indicator-dots
  23. indicator-color= "rgba(20,0,225,1)"
  24. next-margin= "20px"
  25. previous-margin= "20px"
  26. autoplay
  27. bindchange= "pdd">
  28. <swiper-item wx:for=" {{imgArr}} " wx:key="this" > <!-- 循环imgArr里的内容 -->
  29. <image src=" {{item}} " bindtap="getima" data-cc=" {{index}} " >
  30. <!--image src=" {{item}} 含义: imgArr变量里的内容,如本文定义的图片地址 -->
  31. <!-- 将下标给到本地数据库data,并且命名cc -->
  32. </image>
  33. </swiper-item >
  34. </swiper>

复制代码


  
  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. image: [ "/images/0.jpg", "/images/1.jpg", "/images/2.jpeg"],
  7. imgArr:[ "/images/0.jpg", "/images/1.jpg", "/images/2.jpeg"],
  8. pdd: 0,
  9. },
  10. getImg( ) {
  11. var _this = this;
  12. wx. chooseImage({
  13. count: 3, //选择1张,最多选择9张
  14. sizeType: [ 'original', 'compressed'], //是否原图
  15. sourceType: [ 'album', 'camera'], //是否用相机还是相册
  16. success( res) {
  17. // tempFilePath可以作为img标签的src属性显示图片
  18. const tempFilePaths = res. tempFilePaths
  19. _this. setData({
  20. image: res. tempFilePaths,
  21. })
  22. }
  23. })
  24. },
  25. getc( ) {
  26. var acc= this;
  27. wx. chooseImage({
  28. count: 1, //选择1张,最多选择9张
  29. sizeType: [ 'original', 'compressed'], //是否原图
  30. sourceType: [ 'album', 'camera'], //是否用相机还是相册
  31. success( res) {
  32. // tempFilePath可以作为img标签的src属性显示图片
  33. const tempFilePaths = res. tempFilePaths
  34. console. log(tempFilePaths);
  35. acc. data. image. push([tempFilePaths. toString()])
  36. // 在数组image后面增加图片
  37. console. log(acc. data. image);
  38. acc. setData({
  39. image:acc. data. image
  40. })
  41. }
  42. })
  43. },
  44. getima( e){
  45. var _this= this;
  46. //1.拿到我点击的图片下标
  47. console. log(e);
  48. // //2.把下标赋值给ac
  49. var ac= parseInt(e. currentTarget. dataset. cc);
  50. // console.log(ac);
  51. // console.log(this.data.pdd);
  52. wx. chooseImage({
  53. count: 3, //选择1张,最多选择9张
  54. sizeType: [ 'original', 'compressed'], //是否原图
  55. sourceType: [ 'album', 'camera'], //是否用相机还是相册
  56. success( res) {
  57. // tempFilePath可以作为img标签的src属性显示图片
  58. const tempFilePaths = res. tempFilePaths
  59. // 3.将选择的图片的路径,赋值给imgArr
  60. _this. data. imgArr[ac]=res. tempFilePaths[ 0]
  61. // _this.data.imgArr[_this.data.pdd]=res.tempFilePaths[0]
  62. _this. setData({
  63. //4.将存在_this.data.imgArr的路径,赋值到imgArr
  64. imgArr: _this. data. imgArr,
  65. })
  66. }
  67. })
  68. },
  69. pdd( e){
  70. // console.log(e.detail.current);
  71. this. setData({
  72. pdd:e. detail. current
  73. })
  74. }
  75. })

复制代码

这里pdd(e)使用的是第二种方法(不需要可以删除),将所要修改的图片信息赋值给data:{}定义的pdd,此时_this.data.imgArr[_this.data.pdd]=res.tempFilePaths[0]这行里的_this.data.pdd为轮播图里的第几个图片,将要替换的图片的数据,替换近imArr[]里的第几个(_this.data.pdd)图片,最后_this.setData进行替换

通过console.log输出的数据,看到将下标写入了本地数据,并且命名为cc

 


转载:https://blog.csdn.net/weixin_43167662/article/details/128297165
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场