效果图:

1. 新建一个index页面

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

导航条:

  <view class='menu'>
<scroll-view scroll-x>
<view class='scroll-nav'>
<navigator url="/pages/baidupage/baidupage" data-navindex='0' bindtap="navNews" class="{{currentTab!=0?'color-black': 'color-green'}}">社会新闻</navigator>
<navigator url="" data-navindex='1' bindtap="navNews" class="{{currentTab!=1?'color-black': 'color-green'}}">国际新闻</navigator>
<navigator url="" data-navindex='2' bindtap="navNews">国内新闻</navigator>
<navigator url="" data-navindex='3' bindtap="navNews">娱乐新闻</navigator>
<navigator url="" data-navindex='4' bindtap="navNews">体育新闻</navigator>
<navigator url="" data-navindex='5' bindtap="navNews">综合新闻</navigator>
</view>
</scroll-view>
</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下面时,字体是绿色,当切换到其他导航时,颜色变为黑色。

轮播图:

  <view>
<swiper current="{{currentTab}}" bindchange="swiperView">
<swiper-item>
<view class="swiper-view1">社会新闻</view>
</swiper-item>
<swiper-item>
<view class="swiper-view2">国际新闻</view>
</swiper-item>
</swiper>
</view>

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

总的代码:

app.json

{
"pages": [
"pages/index/index",
"pages/baidupage/baidupage"
],
"window": {
"backgroundColor": "#F6F6F6",
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#F00",
"navigationBarTitleText": "今日头条",
"navigationBarTextStyle": "white"
},
"sitemapLocation": "sitemap97.json"
}

app.js

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

app.wxss

/**app.wxss**/
.container {
height: 100%;
box-sizing: border-box;
} page {
height: 100%;
}

index.js

// miniprogram/pages/index/index.js
Page({ /**
* Page initial data
*/
data: {
currentTab:0
}, //swiper切换操作
swiperView:function(event){
// console.log(event)
var currentIndex = event.detail.current
this.setData({
currentTab: currentIndex
})
}, // 新闻点击操作
navNews:function(event){
// console.log(event)
// console.log(event.currentTarget.dataset.navindex)
var navIndex = event.currentTarget.dataset.navindex
// 需要修改currentTab变量
this.setData({
currentTab:navIndex
})
}, /**
* Lifecycle function--Called when page load
*/
onLoad: function (options) { }, /**
* Lifecycle function--Called when page is initially rendered
*/
onReady: function () { }, /**
* Lifecycle function--Called when page show
*/
onShow: function () { }, /**
* Lifecycle function--Called when page hide
*/
onHide: function () { }, /**
* Lifecycle function--Called when page unload
*/
onUnload: function () { }, /**
* Page event handler function--Called when user drop down
*/
onPullDownRefresh: function () { }, /**
* Called when page reach bottom
*/
onReachBottom: function () { }, /**
* Called when user click on the top right corner to share
*/
onShareAppMessage: function () { }
})

index.json

{
"usingComponents": {}
}

index.wxml

<!--miniprogram/pages/index/index.wxml-->
<view class="container">
<view class='menu'>
<scroll-view scroll-x>
<view class='scroll-nav'>
<navigator url="/pages/baidupage/baidupage" data-navindex='0' bindtap="navNews" class="{{currentTab!=0?'color-black': 'color-green'}}">社会新闻</navigator>
<navigator url="" data-navindex='1' bindtap="navNews" class="{{currentTab!=1?'color-black': 'color-green'}}">国际新闻</navigator>
<navigator url="" data-navindex='2' bindtap="navNews">国内新闻</navigator>
<navigator url="" data-navindex='3' bindtap="navNews">娱乐新闻</navigator>
<navigator url="" data-navindex='4' bindtap="navNews">体育新闻</navigator>
<navigator url="" data-navindex='5' bindtap="navNews">综合新闻</navigator>
</view>
</scroll-view>
</view> <view>
<swiper current="{{currentTab}}" bindchange="swiperView">
<swiper-item>
<view class="swiper-view1">社会新闻</view>
</swiper-item>
<swiper-item>
<view class="swiper-view2">国际新闻</view>
</swiper-item>
</swiper>
</view>
</view>

index.wxss

/* miniprogram/pages/index/index.wxss */
.menu{
background-color: lightcyan;
} .scroll-nav{
background-color: lightcyan;
display: flex;
white-space: nowrap;
font-size: 30rpx;
height: 60rpx;
line-height: 60rpx;
} .scroll-nav navigator{
font-weight: bold;
margin: 0 10rpx;
} .swiper-view1{
width: 100%;
height: 400rpx;
background-color: red;
} .swiper-view2{
width: 100%;
height: 400rpx;
background-color: green;
} .color-black{
color: black;
} .color-green{
color: green;
}

baidupage.wxml

<!--miniprogram/pages/baidupage/baidupage.wxml-->
<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. 阿里云视频云 Retina 多媒体 AI 体验馆开张啦!

    带你体验视频更多可能 海量视频管理难度大?翻库检索特定人物费时费力?视频内容剪辑效率低?您的得力助手"Retina多媒体AI"体验馆已上线.带你感受视频AI黑科技,开启极致智能体验 ...

  2. 【Android编程】Java利用Socket类编写Metasploit安卓载荷辅助模块

    /作者:Kali_MG1937 CSDN博客:ALDYS4 QQ:3496925334/ 注意!此文章被作者标记到 黑历史 专栏中,这意味着本篇文章可能存在 质量低下,流水账文,笔法低质 的问题 为了 ...

  3. MySQL零散知识点(02)

    存储过程 存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql,类似于python中的自定义函数 基本使用 delimiter $$ crea ...

  4. .NET 6 亮点之工作负载,它是统一 .NET 的基础

    随着.NET 6 Preview 5的发布,大家认真的看相关文章或者是动手做一个MAUI示例的时候就会碰到一个新概念工作负载(workload),相关规范参见 https://github.com/d ...

  5. spring boot使用@Async异步注解

    1.java的大部分接口的方法都是串行执行的,但是有些业务场景是不需要同步返回结果的,可以把结果直接返回,具体业务异步执行,也有些业务接口是需要并行获取数据,最后把数据聚合在统一返回给前端. 通常我们 ...

  6. Springboot自定义starter打印sql及其执行时间

    前面写到了通过实现mybatis提供的org.apache.ibatis.plugin.Interceptor接口实现了打印SQL执行时间,并格式化SQL及其参数,如果我们使用的是ssm还得再配置文件 ...

  7. csp-s模拟测试49(9.22)养花(分块/主席树)·折射(神仙DP)·画作

    最近有点头晕........... T1 养花 考场我没想到正解,后来打的主席树,对于每个摸数查找1-(k-1),k-(2k-1)...的最大值,事实上还是很容易被卡的但是没有数据好像还比较友善, 对 ...

  8. 温故知新,使用ASP.NET Core创建Web API,永远第一次

    ASP.NET Core简介 ASP.NET Core是一个跨平台的高性能开源框架,用于生成启用云且连接Internet的新式应用. 使用ASP.NET Core,您可以: 生成Web应用和服务.物联 ...

  9. Linux:CentOS-7常用命令

    查看进程 1. ps -ef | grep #查看进程 ps -ef | grep 名称 #示例 ps -ef | grep docker 2. ps aux #当前所有进程信息 ps aux VSZ ...

  10. html javascript checkbox实现全选功能

    html代码 <input type="checkbox" id="all" />all</input> <input type= ...