开始

热门分类封装组件
image.png
topic-nav.vue
image.png

剪切之前先构造动态的数据

image.png

  1. nav:[
  2. {name:"最新"},
  3. {name:"游戏"},
  4. {name:"打卡"},
  5. {name:"情感"},
  6. {name:"故事"},
  7. {name:"喜爱"},
  8. ]

改成v-for循环。
image.png

  1. <view class="u-f-ajc">
  2. <block v-for="(item,index) in topic.nav" :key="index">
  3. <view class="u-f-ajc">{{item.name}}</view>
  4. </block>
  5. </view>

剪切代码到组件

image.png
image.png
这里往下的所有css复制进去
image.png

image.png

image.png

  1. <style scoped>
  2. .topic-nav {
  3. border-bottom: 1upx solid #EEEEEE;
  4. border-top: 1upx solid #EEEEEE;
  5. padding: 20upx;
  6. }
  7. .topic-nav>view:first-child {
  8. margin-bottom: 10upx;
  9. }
  10. .topic-nav>view:first-child view {
  11. color: #9E9E9E;
  12. }
  13. .topic-nav>view:first-child>view:first-child {
  14. color: #333333;
  15. font-size: 32upx;
  16. }
  17. .topic-nav>view:last-child>view {
  18. flex: 1;
  19. background: #dddddd;
  20. color: #9E9E9E;
  21. border-radius: 10upx;
  22. margin: 0 10upx;
  23. }
  24. </style>

js代码

image.png

  1. <block v-for="(item,index) in nav" :key="index">
  2. <view class="u-f-ajc">{{item.name}}</view>
  3. </block>

image.png

  1. <script>
  2. export default {
  3. props: {
  4. nav: Array
  5. }
  6. }
  7. </script>

引入组件
image.png

  1. import topicNav from '@/components/news/topic-nav.vue';
  2. topicNav

image.png

  1. <!-- 热门分类 -->
  2. <topic-nav :nav="topic.nav"></topic-nav>

测试

image.png

本节代码

topic-nav.vue组件

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

结束