微信小程序--仿京东UI样式顶部导航栏

我们先来看看京东的效果

分析

上端导航栏可以放置多个分类,可滑动

点击导航栏最右端按钮可以查看所有分类,同时背景模糊

内容部分右拉跳转到另外的分类

点击分类时导航栏的滑动部分自动滑动到合适的位置

我的实现

代码展示:

js

 /**

 * categoryView.js - 分类页面

 */

var fakeData = require(‘../../common/fakeData.js‘)

Page(

 {

 data: {

 categories: [‘全部‘],

 currentTab: 0,

 scrollLeftValue: 0,

 isPickerShow: false,

 isBgNeed: false,

 commodities: []

 },

 navbarTap: function (e) {

 //将顶部导航栏自动移动到合适的位置

 var idx = e.currentTarget.dataset.idx;

 this.autoScrollTopNav(idx);

 //自动收回

 if (this.data.isPickerShow) {

 this.navbarBtnClick();

 }

 this.setData({

 currentTab: idx

 })

 },

 /**

 * 导航栏右侧箭头按钮点击事件 - 切换模糊背景开闭状态以及展开栏开闭状态

 */

 navbarBtnClick: function(e) {

 this.data.isBgNeed = !this.data.isPickerShow

 this.setData({

 isBgNeed: this.data.isBgNeed

 })

 this.data.isPickerShow = !this.data.isPickerShow

 this.setData({

 isPickerShow: this.data.isPickerShow,

 })

 },

 /**

 * 页面左右滑动事件 - 构造滑动动画,若当前页面无数据,自动加载,需要完善加载函数

 */

 swiperChange: function (e) {

 var idx = e.detail.current;

 this.autoScrollTopNav(idx);

 this.setData({

 currentTab: e.detail.current,

 })

 //若无数据,自动加载

 if (this.data.commodities[idx].length == 0) {

 this.downloadMoreItem();

 }

 },

 /**

 * 上拉刷新

 */

 updateItem: function(e) {

 var idx = this.data.currentTab;

 this.data.commodities[idx] = [];

 this.downloadMoreItem();

 },

 /**

 * 下载更多数据 - 涉及后台拉取数据,需完善

 */

 downloadMoreItem: function(e) {

 var idx = this.data.currentTab;

 var commodities = this.data.commodities;

 //获取更多数据

 commodities[idx] = commodities[idx].concat(

 fakeData.requestForItemsOfType(commodities[idx].length, 10, this.data.categories[idx])

 );

 this.setData({

 commodities: this.data.commodities

 })

 console.log(this.data.commodities);

 },

 /**

 * 用于自动调整顶部类别滑动栏滑动距离,使滑动到用户可接受的合适位置,但自适应上还未考虑太周到

 * @param {number} idx - The index of currentTap.

 */

 autoScrollTopNav: function (idx) {

 if (idx <= 2) {

 this.data.scrollLeftValue = 0;

 } else {

 this.data.scrollLeftValue = (idx - 2) * 60;

 }

 this.setData({

 scrollLeftValue: this.data.scrollLeftValue

 })

 },

 /**

 * 模糊背景点击事件 - 点击模糊背景取消选择

 */

 bgTap: function(e) {

 if (this.data.isPickerShow) {

 this.navbarBtnClick();

 } else {

 return;

 }

 },

 /**

 * 商品点击事件 - 待完善

 */

 itemTap: function(e) {

 console.log("you selsct type " + this.data.currentTab + " item " + e.currentTarget.dataset.idx);

 },

 /**

 * 生命周期函数--监听页面加载,在加载的时候抓取数据

 */

 onLoad: function (options) {

 //首先获取类别项

 this.data.categories = fakeData.requestForCategories();

 this.setData({

 categories: this.data.categories

 })

 //然后默认请求 全部 分类

 for (var i in this.data.categories) {

 this.data.commodities.push([]);

 }

 this.data.commodities[0] = fakeData.requestForItemsOfType(0, 10);

 console.log(this.data.commodities);

 this.setData({

 commodities: this.data.commodities

 })

 }

})

json

{

 "navigationBarTitleText": "商城"

}

wxml

<!--categoryView.wxml-->

<import src="../../common/wxmlTemplate.wxml" />

<!--顶部分类导航条--> 

<view class="">

 <view class="navbar">

 <view class="nav-1"> 

 <scroll-view scroll-x="true" class="nav-1-left" scroll-left="{{scrollLeftValue}}" wx:if="{{!isPickerShow}}">

 <view wx:for="{{categories}}" data-idx="{{index}}" class="item {{currentTab==index ? ‘active‘ : ‘‘}}" 

 wx:key="unique" bindtap="navbarTap">{{item}}</view>

 </scroll-view>

 <view class="nav-1-left" wx:if="{{isPickerShow}}">

 <view class="item left2Font">{{categories[currentTab]}}</view>

 </view> 

 <!-- <view class="blankblock"></view> -->

 <button class="navbarBtn" bindtap="navbarBtnClick">

 <image src="../../resources/返回.png" class="navbarBtn icon {{isPickerShow ? ‘active‘ : ‘‘}}"></image>

 </button> 

 </view>

 <view class="picker" wx:if="{{isPickerShow}}">

 <view class="spitLine"></view>

 <view class="picker-contain">

 <view wx:for="{{categories}}" data-idx="{{index}}" class="item {{currentTab==index ? ‘active‘ : ‘‘}}" 

 wx:key="unique" bindtap="navbarTap">{{item}}</view>

 </view>

 </view>

 </view>

 <swiper class="itemContainer" bindchange="swiperChange" current="{{currentTab}}">

 <block wx:for="{{categories}}" wx:for-item="cItem" wx:key="unique">

 <swiper-item>

 <scroll-view scroll-y="true" bindscrolltolower="downloadMoreItem" bindscrolltoupper="updateItem">

 <view class="child" bindtap="itemTap" wx:for="{{commodities[currentTab]}}" 

 data-idx="{{index}}" wx:key="unique" wx:if="{{item.category == cItem || cItem == ‘全部‘}}">

 <template is="itemInfoTemplate" data="{{item}}"/>

 </view>

 </scroll-view>

 </swiper-item>

 </block>

 </swiper>

 <!-- 透明遮盖层 -->

 <view class="picker-contain-bg {{isPickerShow? ‘show‘ : ‘‘}}" wx:if="{{isBgNeed}}" bindtap="bgTap"></view>

</view>

wxss

@import "../../common/flexGridStyle.wxss";

@import "../../common/itemShowStyle.wxss";

page { 

 display: flex; 

 flex-direction: column; 

 height: 100%; 

} 

.navbar {

 position: fixed;

 z-index: 2;

 background-color: RGB(247, 247, 247);

 width: 100%

}

.nav-1{ 

 flex: none; 

 display: flex; 

 background: #fff; 

 background-color: RGB(247, 247, 247);

 height: 80rpx;

}

.nav-1 .wrapBar {

 display: flex;

}

.nav-1 .item{ 

 position: relative; 

 text-align: center; 

 font-size: 30rpx;

 line-height: 80rpx; 

 color: gray;

 display: inline-block;

 padding-left: 20rpx;

 padding-right: 20rpx;

} 

.nav-1 .item.active{ 

 color: black;

} 

.nav-1 .item.active:after{ 

 content: ""; 

 display: block; 

 position: absolute; 

 bottom: 0; 

 left: 0; 

 right: 0; 

 height: 4rpx; 

 background: black; 

} 

button {

 background: transparent;

}

button::after {

 border: 0;

}

.navbarBtn {

 flex: 0 0 15%;

}

.navbarBtn.icon {

 transform: rotate(90deg);

 width: 20rpx;

 height: 40rpx;

}

.navbarBtn.icon.active{

 transform: rotate(-90deg);

}

.blankblock {

 flex: 0 0 10%;

}

.nav-1-left {

 white-space: nowrap;

 overflow: hidden;

 flex: 0 0 85%;

}

.picker {

}

.item.left2Font {

 color: black;

}

.spitLine {

 margin: 0;

 height: 2rpx;

 background-color: gray;

}

.picker-contain {

 display: flex;

 flex-wrap: wrap;

 justify-content: center;

}

.picker-contain-bg {

 position: fixed;

 background-color: black;

 transition: all 1s;

 width: 100%;

 height: 100%;

 z-index: 1;

 top: 0;

 opacity: 0

}

.picker-contain-bg.show {

 opacity: 0.5;

}

.picker-contain .item {

 background-color: RGB(215, 215, 215);

 border-radius:20px;

 margin: 20rpx;

 padding: 50rpx;

 padding-top: 10rpx;

 padding-bottom: 10rpx;

 font-size: 30rpx;

 color: RGB(161, 161, 161);

}

.picker-contain .item.active{ 

 color: black;

 background-color: RGB(153, 153, 153);

} 

swiper-item scroll-view {

 display: inline;

}

swiper.itemContainer{

 margin-top: 80rpx;

 height: 1136rpx;

}

作者:极乐叔
链接:http://www.jianshu.com/p/03796e8c4504
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

时间: 2024-08-02 10:57:36

微信小程序--仿京东UI样式顶部导航栏的相关文章

小程序-文章:微信小程序常见的UI框架/组件库总结

ylbtech-小程序-文章:微信小程序常见的UI框架/组件库总结 1.返回顶部 1. 想要开发出一套高质量的小程序,运用框架,组件库是省时省力省心必不可少一部分,随着小程序日渐火爆,各种不同类型的小程序也渐渐更新,其中不乏一些优秀好用的框架/组件库. 1:WeUI  小程序–使用教程 https://weui.io/ 官方介绍:WeUI 是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信内网页和微信小程序量身设计,令用户的使用感知更加统一.小程序开发中最常用到的一款框架,受广大

小程序自定义单页面、全局导航栏

摘要: 小程序开发技巧. 作者:小白 原文:小程序自定义单页面.全局导航栏 Fundebug经授权转载,版权归原作者所有. 需求 产品说小程序返回到首页不太方便,想添加返回首页按钮,UI说导航栏能不能设置背景图片,因为那样设计挺好看的. 需求分析并制定方案 这产品和UI都提需求了,咱也不能反驳哈,所以开始调研,分析可行性方案:1.可以添加悬浮按钮.2.自定义导航栏. 添加悬浮按钮,是看起来是比较简单哈,但是感觉不太优雅,会占据页面的空间,体验也不太好.所以想了下第二种方案,自定义导航栏既可以实现

微信小程序组件构建UI界面小练手 —— 表单登录注册微信小程序

通过微信小程序中丰富的表单组件来完成登录界面.手机快速注册界面.企业用户注册界面的微信小程序设计. 将会用到view视图容器组件.button按钮组件.image图片组件.input输入框组件.checkbox多项选择器组件.switch开关选择组件.navigator页面连接组件等. Ⅰ.登录设计 登录表单中,需输入账号.密码进行登录,在账号.密码输入框中都有友好的提示信息:登录按钮默认是灰色不可用状态,只有输入内容后,才会变为可用状态:在登录按钮下面提供手机快速注册.企业用户注册.找回密码链

微信小程序-隐藏和显示自定义的导航

微信小程序中不能直接操作window对象,document文档,跟html的树结构不相同. 实现类似导航的隐藏显示,如图效果: 点击网络显示或隐藏网络中包含的内容.其他类似. 如果是jquery很方便实现,能直接操作document.在微信小程序中实现思路是:在逻辑层定义变量,通过setData赋值. 方法一:通过变量直接赋值,给每一个要控制显示的view定义变量 .wxml 代码: <!--index.wxml--> <view class="navView" bi

微信小程序覆盖自定义组件样式

小程序官方文档明确指出,引入的第三方自定义组件,是不可以对其进行CSS样式覆盖的,但是我们还想要修改怎么办呢?自定义组件时会之定义个外部类,通过这个外部类来修改样式. 修改https://weapp.iviewui.com/所提供的组件样式为例,它定义了一个外部类接口: 1. 通过第三方组件给出的externalClasses: ['i-class'],来指定自己的样式类 自己的wxml i-class="myrate" <i-rate i-class="myrate&

微信小程序--动态添加class样式

尺寸单位: rpx(responsive pixel): 可以根据屏幕宽度进行自适应.规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则750rpx = 375px = 750物理像素,1rpx = 0.5px = 1物理像素.所以用rpx可解决适配问题 样式导入: /** app.wxss **/ @import "common.wxss"; 内联样式: 框架组件上支持使用 style.class 属性来控制组件的样式. style:静

微信小程序仿猫眼

movie.js Page({ data: { movies:null, scrollTop : 0, scrollHeight:0 }, onLoad: function (options) { // 生命周期函数--监听页面加载 // 这里要非常注意,微信的scroll-view必须要设置高度才能监听滚动事件,所以,需要在页面的onLoad事件中给scroll-view的高度赋值 var that = this; wx.getSystemInfo({ success:function(res

微信小程序button去除默认样式

button { font-size: 28rpx; background-color: #fff; border: none; padding: 0; margin: 0; line-height: 1; } input { outline: none; border: none; list-style: none; } button::after { border: none; } button为覆盖的样式 原文地址:https://www.cnblogs.com/jjmirai/p/959

微信小程序——点击切换样式scroll-view

scroll-view滚动视图点击切换样式 *.wxml <view class="content"> <view class="navbg"> <view class="nav"> <scroll-view class="scroll-view_H" scroll-x="true"> <view class="scroll-view_H&qu