开始

话题的分类页也会用到这个组件
image.png

先来构建data数据
image.png

  1. list:[
  2. {
  3. titlepic:"../../static/demo/topicpic/13.jpeg",
  4. title:"话题名称",
  5. desc:"我是话题描述",
  6. totalnum:50,
  7. todaynum:10
  8. },
  9. {
  10. titlepic:"../../static/demo/topicpic/13.jpeg",
  11. title:"话题名称",
  12. desc:"我是话题描述",
  13. totalnum:50,
  14. todaynum:10
  15. },
  16. {
  17. titlepic:"../../static/demo/topicpic/13.jpeg",
  18. title:"话题名称",
  19. desc:"我是话题描述",
  20. totalnum:50,
  21. todaynum:10
  22. },
  23. {
  24. titlepic:"../../static/demo/topicpic/13.jpeg",
  25. title:"话题名称",
  26. desc:"我是话题描述",
  27. totalnum:50,
  28. todaynum:10
  29. },
  30. {
  31. titlepic:"../../static/demo/topicpic/13.jpeg",
  32. title:"话题名称",
  33. desc:"我是话题描述",
  34. totalnum:50,
  35. todaynum:10
  36. },
  37. {
  38. titlepic:"../../static/demo/topicpic/13.jpeg",
  39. title:"话题名称",
  40. desc:"我是话题描述",
  41. totalnum:50,
  42. todaynum:10
  43. }
  44. ]

循环数据
image.png
image.png

  1. <block v-for="(item,index) in topic.list" :key="index">
  2. <view class="topic-list u-f">
  3. <image :src="item.titlepic" mode="widthFix" lazy-load="true">
  4. </image>
  5. <view class="">
  6. <view class="">#{{item.title}}</view>
  7. <view class="">{{item.desc}}</view>
  8. <view class="">动态 {{item.totalnum}} 今日 {{item.todaynum}}</view>
  9. </view>
  10. </view>
  11. </block>

在真机上调试发现,上下的间距有点小,
image.png
这里改成20
image.png

  1. .topic-list {
  2. padding: 20upx 0;
  3. border-bottom: 1upx solid #EEEEEE;
  4. }

标题向下的间距就改的小一点。
image.png

  1. .topic-new>view:first-child {
  2. padding-bottom: 5upx;
  3. font-size: 32upx;
  4. }

image.png

给整个页面加上高度就会有滚动条

image.png

  1. <scroll-view scroll-y class="list" :style="{height: swiperheight+'px'}">

未封装组件之前,本节代码

  1. <template>
  2. <view>
  3. <news-nav-bar :tabBars="tabBars" :tabIndex="tabIndex" @change-tab="changeTab"></news-nav-bar>
  4. <view>
  5. <swiper class="swiper-box" :style="{height: swiperheight+'px'}" :current="tabIndex" :change="tabChange">
  6. <!-- 关注 -->
  7. <swiper-item>
  8. <scroll-view scroll-y class="list" @scrolltolower="loadmore(index)" @scroll="scroll"
  9. :scroll-top="srcollTopValue" :style="{height: swiperheight+'px'}">
  10. <!-- 列表 -->
  11. <block v-for="(item,index) in guanzhu.list" :ket="index">
  12. <common-list :item="item" :index="index"></common-list>
  13. </block>
  14. <!-- 上拉加载 -->
  15. <load-more :loadtext="guanzhu.loadtext"></load-more>
  16. </scroll-view>
  17. </swiper-item>
  18. <!-- 话题 -->
  19. <swiper-item>
  20. <scroll-view scroll-y class="list" :style="{height: swiperheight+'px'}">
  21. <!-- 搜索框 -->
  22. <view class="search-input">
  23. <input class="uni-input" placeholder-class="icon iconfont icon-sousuo topic-search"
  24. placeholder="搜索内容" />
  25. </view>
  26. <!-- 轮播图 -->
  27. <swiper class="topic-swiper" :indicator-dots="true" :autoplay="true" :interval="3000"
  28. :duration="1000">
  29. <block v-for="(item,index) in topic.swiper" :key="index">
  30. <swiper-item>
  31. <image :src="item.src" mode="widthFix" lazy-load="true"></image>
  32. </swiper-item>
  33. </block>
  34. </swiper>
  35. <!-- 热门分类 -->
  36. <topic-nav :nav="topic.nav"></topic-nav>
  37. <!-- 最近更新 -->
  38. <view class="topic-new">
  39. <view class="">最近更新</view>
  40. <block v-for="(item,index) in topic.list" :key="index">
  41. <view class="topic-list u-f">
  42. <image :src="item.titlepic" mode="widthFix" lazy-load="true">
  43. </image>
  44. <view class="">
  45. <view class="">#{{item.title}}</view>
  46. <view class="">{{item.desc}}</view>
  47. <view class="">动态 {{item.totalnum}} 今日 {{item.todaynum}}</view>
  48. </view>
  49. </view>
  50. </block>
  51. </view>
  52. </scroll-view>
  53. </swiper-item>
  54. </swiper>
  55. </view>
  56. </view>
  57. </template>
  58. <script>
  59. import commonList from '@/components/common/common-list.vue';
  60. import newsNavBar from '@/components/news/news-nav-bar.vue';
  61. import loadMore from '@/components/common/load-more.vue';
  62. import topicNav from '@/components/news/topic-nav.vue';
  63. export default {
  64. components: {
  65. commonList,
  66. newsNavBar,
  67. loadMore,
  68. topicNav
  69. },
  70. data() {
  71. return {
  72. swiperheight: 500,
  73. srcollTopValue: 0, // 默认是0
  74. tabIndex: 1,
  75. tabBars: [{
  76. name: '关注',
  77. id: 'guanzhu'
  78. },
  79. {
  80. name: '话题',
  81. id: 'toppic'
  82. },
  83. ],
  84. guanzhu: {
  85. loadtext: "上拉加载更多",
  86. list: [
  87. // 文字
  88. {
  89. userpic: "../../static/demo/userpic/12.jpg",
  90. username: "哈哈",
  91. sex: 0, //0 男 1 女
  92. age: 25,
  93. isguanzhu: false,
  94. title: "我是标题",
  95. titlepic: "",
  96. video: false,
  97. share: false,
  98. path: "深圳 龙岗",
  99. sharenum: 20,
  100. commentnum: 30,
  101. goodnum: 20
  102. },
  103. // 图文
  104. {
  105. userpic: "../../static/demo/userpic/12.jpg",
  106. username: "哈哈",
  107. sex: 0, //0 男 1 女
  108. age: 25,
  109. isguanzhu: false,
  110. title: "我是标题",
  111. titlepic: "../../static/demo/datapic/13.jpg",
  112. video: false,
  113. share: false,
  114. path: "深圳 龙岗",
  115. sharenum: 20,
  116. commentnum: 30,
  117. goodnum: 20
  118. },
  119. // 视频
  120. {
  121. userpic: "../../static/demo/userpic/12.jpg",
  122. username: "哈哈",
  123. sex: 0, //0 男 1 女
  124. age: 25,
  125. isguanzhu: false,
  126. title: "我是标题",
  127. titlepic: "../../static/demo/datapic/13.jpg",
  128. video: {
  129. looknum: "20w",
  130. long: "2:47"
  131. },
  132. share: false,
  133. path: "深圳 龙岗",
  134. sharenum: 20,
  135. commentnum: 30,
  136. goodnum: 20
  137. },
  138. // 分享
  139. {
  140. userpic: "../../static/demo/userpic/12.jpg",
  141. username: "哈哈",
  142. sex: 0, //0 男 1 女
  143. age: 25,
  144. isguanzhu: false,
  145. title: "我是标题",
  146. titlepic: "",
  147. video: false,
  148. share: {
  149. title: "我是分享的标题",
  150. titlepic: "../../static/demo/datapic/14.jpg"
  151. },
  152. path: "深圳 龙岗",
  153. sharenum: 20,
  154. commentnum: 30,
  155. goodnum: 20
  156. },
  157. ]
  158. },
  159. topic: {
  160. swiper: [{
  161. src: "../../static/demo/banner1.jpg"
  162. },
  163. {
  164. src: "../../static/demo/banner2.jpg"
  165. },
  166. {
  167. src: "../../static/demo/banner3.jpg"
  168. },
  169. ],
  170. nav: [{
  171. name: "最新"
  172. },
  173. {
  174. name: "游戏"
  175. },
  176. {
  177. name: "打卡"
  178. },
  179. {
  180. name: "情感"
  181. },
  182. {
  183. name: "故事"
  184. },
  185. {
  186. name: "喜爱"
  187. },
  188. ],
  189. list: [{
  190. titlepic: "../../static/demo/topicpic/13.jpeg",
  191. title: "话题名称",
  192. desc: "我是话题描述",
  193. totalnum: 50,
  194. todaynum: 10
  195. },
  196. {
  197. titlepic: "../../static/demo/topicpic/13.jpeg",
  198. title: "话题名称",
  199. desc: "我是话题描述",
  200. totalnum: 50,
  201. todaynum: 10
  202. },
  203. {
  204. titlepic: "../../static/demo/topicpic/13.jpeg",
  205. title: "话题名称",
  206. desc: "我是话题描述",
  207. totalnum: 50,
  208. todaynum: 10
  209. },
  210. {
  211. titlepic: "../../static/demo/topicpic/13.jpeg",
  212. title: "话题名称",
  213. desc: "我是话题描述",
  214. totalnum: 50,
  215. todaynum: 10
  216. },
  217. {
  218. titlepic: "../../static/demo/topicpic/13.jpeg",
  219. title: "话题名称",
  220. desc: "我是话题描述",
  221. totalnum: 50,
  222. todaynum: 10
  223. },
  224. {
  225. titlepic: "../../static/demo/topicpic/13.jpeg",
  226. title: "话题名称",
  227. desc: "我是话题描述",
  228. totalnum: 50,
  229. todaynum: 10
  230. }
  231. ]
  232. }
  233. }
  234. },
  235. onLoad() {
  236. uni.getSystemInfo({
  237. success: (res) => {
  238. console.log('当前window高度和窗口高度');
  239. console.log(res.windowHeight);
  240. console.log(res.screenHeight);
  241. let height = res.windowHeight - uni.upx2px(100)
  242. this.swiperheight = height;
  243. }
  244. });
  245. },
  246. methods: {
  247. scroll(e) {
  248. // console.log('scroll事件',e);
  249. this.scrollTopValue = e.detail.scrollTop;
  250. },
  251. // 点击切换
  252. changeTab(index) {
  253. console.log('父页面点击:', index);
  254. this.tabIndex = index;
  255. },
  256. // 滑动事件
  257. tabChange(e) {
  258. this.tabIndex = e.details.current
  259. },
  260. // 上拉加载
  261. loadmore() {
  262. if (this.guanzhu.loadtext != "上拉加载更多") {
  263. return;
  264. }
  265. // 修改状态
  266. this.guanzhu.loadtext = "加载中...";
  267. // 获取数据
  268. setTimeout(() => {
  269. //获取完成
  270. let obj = {
  271. userpic: "../../static/demo/userpic/12.jpg",
  272. username: "哈哈",
  273. sex: 0, //0 男 1 女
  274. age: 25,
  275. isguanzhu: false,
  276. title: "我是标题",
  277. titlepic: "../../static/demo/datapic/13.jpg",
  278. video: false,
  279. share: false,
  280. path: "深圳 龙岗",
  281. sharenum: 20,
  282. commentnum: 30,
  283. goodnum: 20
  284. };
  285. this.guanzhu.list.push(obj);
  286. this.guanzhu.loadtext = "上拉加载更多";
  287. }, 1000);
  288. // this.guanzhu.loadtext="没有更多数据了";
  289. }
  290. }
  291. }
  292. </script>
  293. <style>
  294. .search-input {
  295. /* border: 1upx solid; */
  296. padding: 20upx;
  297. }
  298. .search-input>input {
  299. /* border: 1upx solid; */
  300. background: #F4F4F4;
  301. border-radius: 10upx;
  302. }
  303. .topic-search {
  304. display: flex;
  305. justify-content: center;
  306. font-size: 27upx;
  307. }
  308. .topic-swiper {
  309. padding: 0 20upx 20upx 20upx;
  310. }
  311. .topic-swiper image {
  312. width: 100%;
  313. border-radius: 10upx;
  314. }
  315. .topic-new {
  316. padding: 20upx;
  317. }
  318. .topic-new>view:first-child {
  319. padding-bottom: 5upx;
  320. font-size: 32upx;
  321. }
  322. .topic-list {
  323. padding: 20upx 0;
  324. border-bottom: 1upx solid #EEEEEE;
  325. }
  326. .topic-list>view>view {
  327. color: #A4A4A4;
  328. }
  329. .topic-list>view>view:first-child {
  330. color: #333333;
  331. font-size: 32upx;
  332. }
  333. .topic-list image {
  334. width: 150upx;
  335. height: 150upx;
  336. border-radius: 10upx;
  337. margin-right: 20upx;
  338. }
  339. </style>

封装成组件

image.png

image.png
剪切到组件
image.png

image.png

css剪切过去,只剪切,.topic-list和它后面的css就可以了。

  1. .topic-list {
  2. padding: 20upx 0;
  3. border-bottom: 1upx solid #EEEEEE;
  4. }
  5. .topic-list>view>view {
  6. color: #A4A4A4;
  7. }
  8. .topic-list>view>view:first-child {
  9. color: #333333;
  10. font-size: 32upx;
  11. }
  12. .topic-list image {
  13. width: 150upx;
  14. height: 150upx;
  15. border-radius: 10upx;
  16. margin-right: 20upx;
  17. }

image.png

image.png

  1. <script>
  2. export default{
  3. props:{
  4. item:Object,
  5. index:Number
  6. }
  7. }
  8. </script>

引入组件-注册组件-使用

image.png

  1. import topicList from '@/components/news/topic-list.vue';
  2. topicList

image.png

  1. <topic-list :item="item" :index="index"></topic-list>

image.png

默认重新定位到关注列表
image.png

增加动画效果

从左边淡入

  1. animated fadeInLeft fast

image.png

image.png

本节代码

topic-list组件

  1. <template>
  2. <view class="topic-list u-f animated fadeInLeft fast">
  3. <image :src="item.titlepic" mode="widthFix" lazy-load="true">
  4. </image>
  5. <view class="">
  6. <view class="">#{{item.title}}</view>
  7. <view class="">{{item.desc}}</view>
  8. <view class="">动态 {{item.totalnum}} 今日 {{item.todaynum}}</view>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default{
  14. props:{
  15. item:Object,
  16. index:Number
  17. }
  18. }
  19. </script>
  20. <style scoped>
  21. .topic-list {
  22. padding: 20upx 0;
  23. border-bottom: 1upx solid #EEEEEE;
  24. }
  25. .topic-list>view>view {
  26. color: #A4A4A4;
  27. }
  28. .topic-list>view>view:first-child {
  29. color: #333333;
  30. font-size: 32upx;
  31. }
  32. .topic-list image {
  33. width: 150upx;
  34. height: 150upx;
  35. border-radius: 10upx;
  36. margin-right: 20upx;
  37. }
  38. </style>

news.vue页面

  1. <template>
  2. <view>
  3. <news-nav-bar :tabBars="tabBars" :tabIndex="tabIndex" @change-tab="changeTab"></news-nav-bar>
  4. <view>
  5. <swiper class="swiper-box" :style="{height: swiperheight+'px'}" :current="tabIndex" :change="tabChange">
  6. <!-- 关注 -->
  7. <swiper-item>
  8. <scroll-view scroll-y class="list" @scrolltolower="loadmore(index)" @scroll="scroll"
  9. :scroll-top="srcollTopValue" :style="{height: swiperheight+'px'}">
  10. <!-- 列表 -->
  11. <block v-for="(item,index) in guanzhu.list" :ket="index">
  12. <common-list :item="item" :index="index"></common-list>
  13. </block>
  14. <!-- 上拉加载 -->
  15. <load-more :loadtext="guanzhu.loadtext"></load-more>
  16. </scroll-view>
  17. </swiper-item>
  18. <!-- 话题 -->
  19. <swiper-item>
  20. <scroll-view scroll-y class="list" :style="{height: swiperheight+'px'}">
  21. <!-- 搜索框 -->
  22. <view class="search-input">
  23. <input class="uni-input" placeholder-class="icon iconfont icon-sousuo topic-search"
  24. placeholder="搜索内容" />
  25. </view>
  26. <!-- 轮播图 -->
  27. <swiper class="topic-swiper" :indicator-dots="true" :autoplay="true" :interval="3000"
  28. :duration="1000">
  29. <block v-for="(item,index) in topic.swiper" :key="index">
  30. <swiper-item>
  31. <image :src="item.src" mode="widthFix" lazy-load="true"></image>
  32. </swiper-item>
  33. </block>
  34. </swiper>
  35. <!-- 热门分类 -->
  36. <topic-nav :nav="topic.nav"></topic-nav>
  37. <!-- 最近更新 -->
  38. <view class="topic-new">
  39. <view class="">最近更新</view>
  40. <block v-for="(item,index) in topic.list" :key="index">
  41. <topic-list :item="item" :index="index"></topic-list>
  42. </block>
  43. </view>
  44. </scroll-view>
  45. </swiper-item>
  46. </swiper>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. import commonList from '@/components/common/common-list.vue';
  52. import newsNavBar from '@/components/news/news-nav-bar.vue';
  53. import loadMore from '@/components/common/load-more.vue';
  54. import topicNav from '@/components/news/topic-nav.vue';
  55. import topicList from '@/components/news/topic-list.vue';
  56. export default {
  57. components: {
  58. commonList,
  59. newsNavBar,
  60. loadMore,
  61. topicNav,
  62. topicList
  63. },
  64. data() {
  65. return {
  66. swiperheight: 500,
  67. srcollTopValue: 0, // 默认是0
  68. tabIndex: 0,
  69. tabBars: [{
  70. name: '关注',
  71. id: 'guanzhu'
  72. },
  73. {
  74. name: '话题',
  75. id: 'toppic'
  76. },
  77. ],
  78. guanzhu: {
  79. loadtext: "上拉加载更多",
  80. list: [
  81. // 文字
  82. {
  83. userpic: "../../static/demo/userpic/12.jpg",
  84. username: "哈哈",
  85. sex: 0, //0 男 1 女
  86. age: 25,
  87. isguanzhu: false,
  88. title: "我是标题",
  89. titlepic: "",
  90. video: false,
  91. share: false,
  92. path: "深圳 龙岗",
  93. sharenum: 20,
  94. commentnum: 30,
  95. goodnum: 20
  96. },
  97. // 图文
  98. {
  99. userpic: "../../static/demo/userpic/12.jpg",
  100. username: "哈哈",
  101. sex: 0, //0 男 1 女
  102. age: 25,
  103. isguanzhu: false,
  104. title: "我是标题",
  105. titlepic: "../../static/demo/datapic/13.jpg",
  106. video: false,
  107. share: false,
  108. path: "深圳 龙岗",
  109. sharenum: 20,
  110. commentnum: 30,
  111. goodnum: 20
  112. },
  113. // 视频
  114. {
  115. userpic: "../../static/demo/userpic/12.jpg",
  116. username: "哈哈",
  117. sex: 0, //0 男 1 女
  118. age: 25,
  119. isguanzhu: false,
  120. title: "我是标题",
  121. titlepic: "../../static/demo/datapic/13.jpg",
  122. video: {
  123. looknum: "20w",
  124. long: "2:47"
  125. },
  126. share: false,
  127. path: "深圳 龙岗",
  128. sharenum: 20,
  129. commentnum: 30,
  130. goodnum: 20
  131. },
  132. // 分享
  133. {
  134. userpic: "../../static/demo/userpic/12.jpg",
  135. username: "哈哈",
  136. sex: 0, //0 男 1 女
  137. age: 25,
  138. isguanzhu: false,
  139. title: "我是标题",
  140. titlepic: "",
  141. video: false,
  142. share: {
  143. title: "我是分享的标题",
  144. titlepic: "../../static/demo/datapic/14.jpg"
  145. },
  146. path: "深圳 龙岗",
  147. sharenum: 20,
  148. commentnum: 30,
  149. goodnum: 20
  150. },
  151. ]
  152. },
  153. topic: {
  154. swiper: [{
  155. src: "../../static/demo/banner1.jpg"
  156. },
  157. {
  158. src: "../../static/demo/banner2.jpg"
  159. },
  160. {
  161. src: "../../static/demo/banner3.jpg"
  162. },
  163. ],
  164. nav: [{
  165. name: "最新"
  166. },
  167. {
  168. name: "游戏"
  169. },
  170. {
  171. name: "打卡"
  172. },
  173. {
  174. name: "情感"
  175. },
  176. {
  177. name: "故事"
  178. },
  179. {
  180. name: "喜爱"
  181. },
  182. ],
  183. list: [{
  184. titlepic: "../../static/demo/topicpic/13.jpeg",
  185. title: "话题名称",
  186. desc: "我是话题描述",
  187. totalnum: 50,
  188. todaynum: 10
  189. },
  190. {
  191. titlepic: "../../static/demo/topicpic/13.jpeg",
  192. title: "话题名称",
  193. desc: "我是话题描述",
  194. totalnum: 50,
  195. todaynum: 10
  196. },
  197. {
  198. titlepic: "../../static/demo/topicpic/13.jpeg",
  199. title: "话题名称",
  200. desc: "我是话题描述",
  201. totalnum: 50,
  202. todaynum: 10
  203. },
  204. {
  205. titlepic: "../../static/demo/topicpic/13.jpeg",
  206. title: "话题名称",
  207. desc: "我是话题描述",
  208. totalnum: 50,
  209. todaynum: 10
  210. },
  211. {
  212. titlepic: "../../static/demo/topicpic/13.jpeg",
  213. title: "话题名称",
  214. desc: "我是话题描述",
  215. totalnum: 50,
  216. todaynum: 10
  217. },
  218. {
  219. titlepic: "../../static/demo/topicpic/13.jpeg",
  220. title: "话题名称",
  221. desc: "我是话题描述",
  222. totalnum: 50,
  223. todaynum: 10
  224. }
  225. ]
  226. }
  227. }
  228. },
  229. onLoad() {
  230. uni.getSystemInfo({
  231. success: (res) => {
  232. console.log('当前window高度和窗口高度');
  233. console.log(res.windowHeight);
  234. console.log(res.screenHeight);
  235. let height = res.windowHeight - uni.upx2px(100)
  236. this.swiperheight = height;
  237. }
  238. });
  239. },
  240. methods: {
  241. scroll(e) {
  242. // console.log('scroll事件',e);
  243. this.scrollTopValue = e.detail.scrollTop;
  244. },
  245. // 点击切换
  246. changeTab(index) {
  247. console.log('父页面点击:', index);
  248. this.tabIndex = index;
  249. },
  250. // 滑动事件
  251. tabChange(e) {
  252. this.tabIndex = e.details.current
  253. },
  254. // 上拉加载
  255. loadmore() {
  256. if (this.guanzhu.loadtext != "上拉加载更多") {
  257. return;
  258. }
  259. // 修改状态
  260. this.guanzhu.loadtext = "加载中...";
  261. // 获取数据
  262. setTimeout(() => {
  263. //获取完成
  264. let obj = {
  265. userpic: "../../static/demo/userpic/12.jpg",
  266. username: "哈哈",
  267. sex: 0, //0 男 1 女
  268. age: 25,
  269. isguanzhu: false,
  270. title: "我是标题",
  271. titlepic: "../../static/demo/datapic/13.jpg",
  272. video: false,
  273. share: false,
  274. path: "深圳 龙岗",
  275. sharenum: 20,
  276. commentnum: 30,
  277. goodnum: 20
  278. };
  279. this.guanzhu.list.push(obj);
  280. this.guanzhu.loadtext = "上拉加载更多";
  281. }, 1000);
  282. // this.guanzhu.loadtext="没有更多数据了";
  283. }
  284. }
  285. }
  286. </script>
  287. <style>
  288. .search-input {
  289. /* border: 1upx solid; */
  290. padding: 20upx;
  291. }
  292. .search-input>input {
  293. /* border: 1upx solid; */
  294. background: #F4F4F4;
  295. border-radius: 10upx;
  296. }
  297. .topic-search {
  298. display: flex;
  299. justify-content: center;
  300. font-size: 27upx;
  301. }
  302. .topic-swiper {
  303. padding: 0 20upx 20upx 20upx;
  304. }
  305. .topic-swiper image {
  306. width: 100%;
  307. border-radius: 10upx;
  308. }
  309. .topic-new {
  310. padding: 20upx;
  311. }
  312. .topic-new>view:first-child {
  313. padding-bottom: 5upx;
  314. font-size: 32upx;
  315. }
  316. </style>

结束