效果图:

1. 新建一个index页面

主页面分为两块,上面是导航条,下面是轮播图。

导航条:

  1. <view class='menu'>
  2. <scroll-view scroll-x>
  3. <view class='scroll-nav'>
  4. <navigator url="/pages/baidupage/baidupage" data-navindex='0' bindtap="navNews" class="{{currentTab!=0?'color-black': 'color-green'}}">社会新闻</navigator>
  5. <navigator url="" data-navindex='1' bindtap="navNews" class="{{currentTab!=1?'color-black': 'color-green'}}">国际新闻</navigator>
  6. <navigator url="" data-navindex='2' bindtap="navNews">国内新闻</navigator>
  7. <navigator url="" data-navindex='3' bindtap="navNews">娱乐新闻</navigator>
  8. <navigator url="" data-navindex='4' bindtap="navNews">体育新闻</navigator>
  9. <navigator url="" data-navindex='5' bindtap="navNews">综合新闻</navigator>
  10. </view>
  11. </scroll-view>
  12. </view>

scroll-x表示可以横向滚动,导航条内容较长,因此需要横向滚动。

navigator是导航条,

url里面是跳转链接,这个链接页面必须在app.json里面存在。当点击这个导航时会跳转到baidupage的页面,这个页面里面的内容是打开https://wap.baidu.com,一般小程序是没有权限打开百度页面的,这里是模拟,所以关闭了验证,具体原因参考https://blog.csdn.net/qq_32113629/article/details/79483213

bindtap是绑定事件,当点击这个导航时会触发navNews函数,这个函数在Index.js中定义的。

class里面是导航的颜色显示,当在当前tab下面时,字体是绿色,当切换到其他导航时,颜色变为黑色。

轮播图:

  1. <view>
  2. <swiper current="{{currentTab}}" bindchange="swiperView">
  3. <swiper-item>
  4. <view class="swiper-view1">社会新闻</view>
  5. </swiper-item>
  6. <swiper-item>
  7. <view class="swiper-view2">国际新闻</view>
  8. </swiper-item>
  9. </swiper>
  10. </view>

社会新闻的页面是swiper-view1,为红色。国际新闻的页面是swiper-view2,为绿色,其中currentTab这个变量就是个关键,将导航条和轮播图联系起来。

总的代码:

app.json

  1. {
  2. "pages": [
  3. "pages/index/index",
  4. "pages/baidupage/baidupage"
  5. ],
  6. "window": {
  7. "backgroundColor": "#F6F6F6",
  8. "backgroundTextStyle": "light",
  9. "navigationBarBackgroundColor": "#F00",
  10. "navigationBarTitleText": "今日头条",
  11. "navigationBarTextStyle": "white"
  12. },
  13. "sitemapLocation": "sitemap97.json"
  14. }

app.js

  1. //app.js
  2. App({
  3. onLaunch: function () {
  4.  
  5. if (!wx.cloud) {
  6. console.error('请使用 2.2.3 或以上的基础库以使用云能力')
  7. } else {
  8. wx.cloud.init({
  9. // env 参数说明:
  10. // env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
  11. // 此处请填入环境 ID, 环境 ID 可打开云控制台查看
  12. // 如不填则使用默认环境(第一个创建的环境)
  13. // env: 'my-env-id',
  14. traceUser: true,
  15. })
  16. }
  17.  
  18. this.globalData = {}
  19. }
  20. })

app.wxss

  1. /**app.wxss**/
  2. .container {
  3. height: 100%;
  4. box-sizing: border-box;
  5. }
  6.  
  7. page {
  8. height: 100%;
  9. }

index.js

  1. // miniprogram/pages/index/index.js
  2. Page({
  3.  
  4. /**
  5. * Page initial data
  6. */
  7. data: {
  8. currentTab:0
  9. },
  10.  
  11. //swiper切换操作
  12. swiperView:function(event){
  13. // console.log(event)
  14. var currentIndex = event.detail.current
  15. this.setData({
  16. currentTab: currentIndex
  17. })
  18. },
  19.  
  20. // 新闻点击操作
  21. navNews:function(event){
  22. // console.log(event)
  23. // console.log(event.currentTarget.dataset.navindex)
  24. var navIndex = event.currentTarget.dataset.navindex
  25. // 需要修改currentTab变量
  26. this.setData({
  27. currentTab:navIndex
  28. })
  29. },
  30.  
  31. /**
  32. * Lifecycle function--Called when page load
  33. */
  34. onLoad: function (options) {
  35.  
  36. },
  37.  
  38. /**
  39. * Lifecycle function--Called when page is initially rendered
  40. */
  41. onReady: function () {
  42.  
  43. },
  44.  
  45. /**
  46. * Lifecycle function--Called when page show
  47. */
  48. onShow: function () {
  49.  
  50. },
  51.  
  52. /**
  53. * Lifecycle function--Called when page hide
  54. */
  55. onHide: function () {
  56.  
  57. },
  58.  
  59. /**
  60. * Lifecycle function--Called when page unload
  61. */
  62. onUnload: function () {
  63.  
  64. },
  65.  
  66. /**
  67. * Page event handler function--Called when user drop down
  68. */
  69. onPullDownRefresh: function () {
  70.  
  71. },
  72.  
  73. /**
  74. * Called when page reach bottom
  75. */
  76. onReachBottom: function () {
  77.  
  78. },
  79.  
  80. /**
  81. * Called when user click on the top right corner to share
  82. */
  83. onShareAppMessage: function () {
  84.  
  85. }
  86. })

index.json

  1. {
  2. "usingComponents": {}
  3. }

index.wxml

  1. <!--miniprogram/pages/index/index.wxml-->
  2. <view class="container">
  3. <view class='menu'>
  4. <scroll-view scroll-x>
  5. <view class='scroll-nav'>
  6. <navigator url="/pages/baidupage/baidupage" data-navindex='0' bindtap="navNews" class="{{currentTab!=0?'color-black': 'color-green'}}">社会新闻</navigator>
  7. <navigator url="" data-navindex='1' bindtap="navNews" class="{{currentTab!=1?'color-black': 'color-green'}}">国际新闻</navigator>
  8. <navigator url="" data-navindex='2' bindtap="navNews">国内新闻</navigator>
  9. <navigator url="" data-navindex='3' bindtap="navNews">娱乐新闻</navigator>
  10. <navigator url="" data-navindex='4' bindtap="navNews">体育新闻</navigator>
  11. <navigator url="" data-navindex='5' bindtap="navNews">综合新闻</navigator>
  12. </view>
  13. </scroll-view>
  14. </view>
  15.  
  16. <view>
  17. <swiper current="{{currentTab}}" bindchange="swiperView">
  18. <swiper-item>
  19. <view class="swiper-view1">社会新闻</view>
  20. </swiper-item>
  21. <swiper-item>
  22. <view class="swiper-view2">国际新闻</view>
  23. </swiper-item>
  24. </swiper>
  25. </view>
  26. </view>

index.wxss

  1. /* miniprogram/pages/index/index.wxss */
  2. .menu{
  3. background-color: lightcyan;
  4. }
  5.  
  6. .scroll-nav{
  7. background-color: lightcyan;
  8. display: flex;
  9. white-space: nowrap;
  10. font-size: 30rpx;
  11. height: 60rpx;
  12. line-height: 60rpx;
  13. }
  14.  
  15. .scroll-nav navigator{
  16. font-weight: bold;
  17. margin: 0 10rpx;
  18. }
  19.  
  20. .swiper-view1{
  21. width: 100%;
  22. height: 400rpx;
  23. background-color: red;
  24. }
  25.  
  26. .swiper-view2{
  27. width: 100%;
  28. height: 400rpx;
  29. background-color: green;
  30. }
  31.  
  32. .color-black{
  33. color: black;
  34. }
  35.  
  36. .color-green{
  37. color: green;
  38. }

baidupage.wxml

  1. <!--miniprogram/pages/baidupage/baidupage.wxml-->
  2. <web-view src="https://wap.baidu.com/"></web-view>

OK.

navigator导航页面跳转与绑定事件的更多相关文章

  1. jquerymobile页面跳转和参数传递

    http://blog.csdn.net/chen052210123/article/details/7481578 页面跳转: 页面跳转时pagebeforechange事件会被触发两次,通过$(d ...

  2. 微信小程序开发:学习笔记[8]——页面跳转及传参

    微信小程序开发:学习笔记[8]——页面跳转及传参 页面跳转 一个小程序拥有多个页面,我们可以通过wx.navigateTo推入一个新的页面.在首页使用2次wx.navigateTo后,页面层级会有三层 ...

  3. jquery html 动态添加元素绑定事件

    由于实际的需要,有时需要往网页中动态的插入HTML内容,并在插入的节点中绑定事件处理函数.我们知道,用Javascript向HTML文档中 插入内容,有两种方法, 一种是在写HTML代码写入JS,然后 ...

  4. react-native-http请求后navigator导航跳转

    琢磨react-native有一段时间了.对于我来说,它的确是前端开发工作者的福音,因为我可以利用它来写app的代码,而且基本可以一套代码,多个平台使用. 早就想写一篇随笔记录一下react nati ...

  5. Android实现页面跳转、ListView及其事件

    Android实现页面跳转.ListView及其事件 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 进入主页面后,使用ListView实现特 ...

  6. 原创+转发:微信小程序navigator、redirectTo、switchTab几种页面跳转方式

    什么是事件? 事件是视图层到逻辑层的通讯方式. 事件可以将用户的行为反馈到逻辑层进行处理. 详解(以常见的tap点击事情为例) 模板.wxml代码: <view id="tapTest ...

  7. 【Flutter学习】页面跳转之路由及导航

    一,概述 移动应用通常通过成为‘屏幕’或者‘页面’的全屏元素显示其内容,在Flutter中,这些元素统称为路由,它们由导航器Navigator组件管理.导航器管理一组路由Route对象,并提供了管理堆 ...

  8. Flutter 使用Navigator进行局部跳转页面

    老孟导读:Navigator组件使用的频率不是很高,但在一些场景下非常适用,比如局部表单多页填写.底部导航一直存在,每个tab各自导航场景. Navigator 是管理路由的控件,通常情况下直接使用N ...

  9. android 学习第一天 了解事件机制,页面跳转等常用操作

    点击时间2种 第一种,通过初始化页面 写入点击事件 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedI ...

随机推荐

  1. DarkGreenTrip博客搭建成功

    本博客(https://www.cnblogs.com/zhangshuhao1116)自2021年6月19日由 Shu-How Z  搭建成功,2018年搭建过hexo+next.Wordpress ...

  2. 【题解】Luogu P2889 [USACO07NOV]挤奶的时间Milking Time

    Luogu P2889 [USACO07NOV]挤奶的时间Milking Time 题目描述 传送门Bessie is such a hard-working cow. In fact, she is ...

  3. php 安装 yii 报错: phpunit/phpunit 4.8.32 requires ext-dom *

    php 安装 yii 报错: phpunit/phpunit 4.8.32 requires ext-dom * 我的版本是7.0,以7.0为例演示. 先装这两个拓展试试: sudo apt-get ...

  4. MySQL密码复杂度策略

    前言 MySQL5.6.6版本之后增加了密码强度验证插件validate_password,相关参数设置的较为严格.使用了该插件会检查设置的密码是否符合当前设置的强度规则,若不满足则拒绝设置. 本文采 ...

  5. 什么IP欺骗?

    1.什么是IP欺骗? IP欺骗是指创建源地址经过修改的Internet协议(IP) 数据包,目的要么是隐藏发送方的身份,要么是冒充其他计算机系统,或者两者兼具.恶意用户往往采用这项技术对目标设备或周边 ...

  6. 区分DDD中的Domain, Subdomain, Bounded Context, Problem/Solution Space

    区分DDD中的Domain, Subdomain, Bounded Context, Problem/Solution Space 译自: Domain, Subdomain, Bounded Con ...

  7. flex中Button事件中的e.target

    关于flex中的Button事件中的e.target. 今天想在事件中调用模块中的对象通过e.target获取单击的这个Button对象,但是可能是使用var btn:Button = e.targe ...

  8. Maven安装、配置及基础

    简介: Maven是Apache公司的开源项目,是项目构建工具,用来管理依赖. Maven的优点: 同样的代码实现相同的功能,Maven项目没有Jar包,项目大小更小. maven的优点如何实现: 没 ...

  9. 初入web前端---实习(职场菜鹏)

    作为一个大四的准职场新人,顺利的找到了一份自己想从事的工作---web前端开发.

  10. CentOS-查找删除历史文件

    背景:因服务器磁盘空间有限,根据实际情况控制保留指定的几天内的历史文件 find参数说明: /home/tmp        设置查找的目录 -mtime +30       设置修改时间为30天前 ...