618技术揭秘|探究竞速榜页面核心前端技术
前 言
H5页面作为移动端Web应用的重要形式之一,已经成为了现代Web开发的热门话题。在H5页面的开发过程中,前端技术的应用至关重要。本文将探究京东竞速榜H5页面的核心前端技术,包括动画、样式配置化、皮肤切换、海报技术、调试技巧等方面,希望能够为广大前端开发者提供一些有用的参考和思路。
<div class="progress"></div><!--* 进度条 *-->.progress {z-index: 10;height: 7px;background-image: @progress-bar-color;border-radius: 8px;/* 这是一个CSS动画,包含两个动画效果:ProgressRise和moveScaleRight。这两个动画效果是通过CSS的animation属性同时应用在同一个元素上实现的。其中,animation属性可以接受多个动画效果,用逗号分隔。在这个例子中,表示同时应用ProgressRise和moveScaleRight两个动画效果。*/animation: ProgressRise 2s ease, moveScaleRight 2s 4.6s alternate ease-out infinite;transform-origin: left center;}/* 当页面首屏渲染进度条的宽度 */@keyframes ProgressRise {0% {width: 0;}100% {width: 100%;}}@keyframes moveScaleRight {from {transform: translate3d(0, 0, 0) scale3d(1, 1, 1) skewX(0deg);} to {transform: translate3d(0, 0, 0) scale3d(1.04, 1, 1) skewX(0deg);}}
<div class="light-theme-0"></div><!--* 排名徽章 *-->.light-theme-0 {position: absolute;top: -6px;right: -34px;width: 22px;height: 22px;background: url('~@/assets/images/bcmixin/1.png') no-repeat center center;background-size: 100% 100%;//这段代码为元素添加了一个名为lightMoveRight的动画效果,持续时间为2秒,时间函数为ease-out,延迟时间为4.6秒,重复次数为无限次,并且使用了alternate属性,使得动画在重复播放时会反向播放。animation: lightMoveRight 2s ease-out 4.6s alternate infinite; //右侧弹性效果动画transform-origin: left center;}@keyframes lightMoveRight {from {transform: translateX(0px);}to {transform: translateX(-8px);}}
菜单滚动动画
<div class="menu" style="transform: translateX(257px); transition-duration: 0.3s;"><ul><li>菜单项1</li>...</ul></div>document.querySelector('.menu').addEventListener('touchmove', function () {var transformValue = window.getComputedStyle(menu).getPropertyValue('transform')var translateXValue = parseInt(transformValue.split(',')[4])var scrollDistance = 100menu.style.transform = 'translateX(' + (translateXValue + scrollDistance) + 'px)'})
按钮过渡动画
为了让用户可以一屏查看更多关键信息,新增了简洁版页面。切换简洁版时,按钮采用了过渡动画。为按钮元素添加了一个过渡效果,使得元素的所有属性在1秒内发生变化时会平滑过渡。其中当点击按钮时,将会按照1秒时间平滑更换背景图片。
<div class="bc-controll-zoom-wrap bc-controll-zoom-wrap-deep"></div>.bc-controll-zoom-wrap {position: fixed;bottom: 30px;right: 0;width: 83px;height: 54px;background: url('../../assets/images/float_btn_2023_618.png');background-size: 100% 100%;z-index: 1999;display: flex;justify-content: center;align-items: center;//all表示所有属性都会发生过渡,1s表示过渡的持续时间为1秒transition: all 1s;}.bc-controll-zoom-wrap-deep {background: url('../../assets/images/float_btn_deep_2023_618.png');background-size: 100% 100%;}
弹框由小变大动画
.zoomIn-enter-active {animation: zoomIn 0.3s;}@keyframes zoomIn {0% {opacity: 0;-webkit-transform: scale3d(0.3, 0.3, 0.3);transform: scale3d(0.3, 0.3, 0.3);}50% {opacity: 1;}
// Animation 定义CSS动画效果.animation(@animation) {-webkit-animation:@animation;-moz-animation:@animation;-o-animation:@animation;animation:@animation;}
页面自动生成海报可以提高用户的互动性和分享性,增强品牌的曝光度和口碑效应,提高用户留存率和转化率。将网页的重要信息自动生成海报,并加上二维码方便竞速榜网页分享和传播。那么纯前端怎么画海报这块,就是一个关键的技术。竞速榜采用了通过 css 属性画 canvas 图片的轻量级的 vue 组件Vue Canvas Poster。它可以使用类css属性的方式,按照绝对定位布局生成canvas图。并且可以通过widthPixels设置生成图片尺寸,解决图片模糊问题。其中image属性用来绘制图片,text用来绘制文本,rect用来绘制矩形,qrcode用来绘制二维码。根据width 宽度、height高度、top上边距、left左边距,来组装对应元素位置,绘制海报内容。
import { VueCanvasPoster } from 'vue-canvas-poster'export default {components: {VueCanvasPoster,},}this.painting = {width: '334px',height: '600px',top: 0,background: backgroundImage,views: [{// 小标题背景type: 'image',url: subTitleBackgroundImage,css: {top: '125px',left: '16px',width: '297px',height: '70px',},},//页面文本{type: 'text',text: currTitle,css: [{left: '23px',top: '140px',fontFamily: 'PingFangSC-Semibold',fontSize: currTitleFontSize,color: textColor,width: '290px',height: '33px',textAlign: 'center',},],},...{// 底部白色背景type: 'rect',css: {bottom: '10px',right: '24px',width: '53px',height: '53px',color: '#fff',},},//二维码{type: 'qrcode',content: qrcodeSrc,css: {bottom: '14px',right: '28px',width: '44px',height: '44px',color: '#000',}, },],}
/* 颜色变量 */@primary-color: #007bff;/* 字体变量 */@font-size-base: 16px;/* 边框变量 */@border-color: #ccc;/* 布局变量 */@container-width: 1200px;/* 动画变量 */@animation-duration: 0.3s;/* 响应式变量 */@screen-xs: 576px;
//背景颜色this.posterConfig = getConfigBySkin(this.$store.state.skin)let getConfigBySkin = (skin) => {// 默认背景let config = {globalBg:'#3F2786', //背景图purpleTitleBg: require('../../../assets/images/skin_purple/bg-time-purple.png'), //标题背景图rightArrow: require('../../../assets/images/skin_purple_pre/arrow-right.png'),titleBg: require('../../../assets/images/skin_common/purple-title-border.png'),number: 'skin_purple',progressBar: require('../../../assets/images/skin_common/purple-bar.png'),leftBg: require('../../../assets/images/skin_common/digital-l-b.png'),rightBg: require('../../../assets/images/skin_common/digital-r-t.png'),}//获取背景图片'red'、'golden'、'purple'、'blue'switch (skin) {case 'red':config.globalBg = '#780605'config.number = 'skin_red'...breakcase 'blue':...breakcase 'golden':...break}return config}
新增vconsole
/*** 参数携带了vconsole,就启用vconsole*/import VConsole from 'vconsole'if (location.href.indexOf('vconsole') !== -1) {import('vconsole').then(({ default: VConsole }) => {new VConsole();})}
新增nojump不跳转
/*** 返回结果处理控制器** @param {function} resolve promise resolve function* @param {function} reject promise reject function* @param {object} result 接口返回数据* @param {boolean} isNeedReject 是否需要执行reject* @param {string} url 请求的接口地址*/const handlerResult = (resolve, reject, result, isNeedReject, url) => {//是否需要跳转const nojump = getQueryString('nojump') === '1'switch (result.code) {//时间小于“活动对外时间”或者时间大于“活动下线时间”case '103':if (nojump) {console.log('nojump非跳转模式', '状态码:103', '时间小于“活动对外时间”或者时间大于“活动下线时间”', '请求接口:', url, '当前页面地址:', location.href)} else {window.location.href = '//www.jd.com'}breakcase '200':resolve(result.data)break //未登录ERP / 登录异常 / 活动类型有误case '401':window.location.href = `//ssa.jd.com/sso/login?ReturnUrl=${encodeURIComponent(location.href)}`break...}}
为提高开发效率,需要新增多个快捷启动命令如下所示:
启动本地mock数据环境,方便后端接口未开发好时,前端Mock数据并行开发。
启动测试环境数据环境,方便与后端进行连调。
启动线上数据,模拟用户真实环境排查问题。
编译测试环境
编译线上环境
"start:dev": "node script/switch_env_server.js dev && cross-env BUILD_ENV=dev vue-cli-service serve --open","start:prod": "node script/switch_env_server.js prod && cross-env vue-cli-service serve --open","start:mock": "node script/switch_env_server.js local && cross-env MOCK=true vue-cli-service serve --open","build:dev": "node script/switch_env_build.js dev && cross-env BUILD_ENV=dev npm run oss","build:prod": "node script/switch_env_build.js prod && cross-env BUILD_ENV=prod npm run oss"
新增ump监控,每分钟会去访问页面链接的连通性,判断页面是否返回除了200、301、302的异常状态码,如果连续3次页面访问异常。如返回异常状态码,则通过公司内部聊天工具咚咚、邮件等方式进行消息推送。
总 结
618技术大揭秘:Switchquery秒级配置触达平台的设计与实现