需求背景
试想一下,假如网站的页面内容比较丰富,整个页面的长度将会随之变长。顾客已经翻到了比较靠后的位置,想快速的返回到上面的某个模块去深度参与一下,有的需要滑动半天,无形之中加重了参与的难度,消磨顾客的耐心。
我们再带入一个场景,黑五活动大促。其中包括的品类比较多,商品组合也较多。那么如何让顾客能够快速锁定到目标品类呢?
这个时候如果我们能够给出一个快捷导航的方案,就会便利很多。现存的快捷导航很多都是需要代码深度参与,如何减少代码深度参与,让新手也能轻松创建,是我们需要关心的问题。
1. 实现原理
在section中给楼层添加标记位,计算当前滚动条距离楼层标记位置的高度,如果楼层满足条件则激活楼层。
2. 如何实现
按照我们的实现原理只需要添加2个文件就可以实现此功能。并且这个实现和主题无关。
Section : floor.liquid , floor-switch.liquid
<div class="floor-scope" style="display: none;" data-floor-name="{{ section.settings.name }}"></div>{% schema %}{"name": "楼层","tag": "section","class": "floor-section","settings": [{"type": "text","id": "name","label": "楼层名称"}],"presets": [{"name": "楼层"}]}{% endschema %}
<style>#floor-nav {position: fixed;left: 0;z-index: 999;left: 50px;top: 40%;-webkit-transform: translate(-50%, -50%);transform: translate(-50%, -50%);display: none;background: #eee;}#floor-nav .floor-nav-item {width: 6vh;height: 6vh;line-height: 6vh;text-align: center;cursor: pointer;position: relative;font-weight: bold;}#floor-nav .item__name {display: none;color: darkred;font-size: 16px;position: absolute;left: 6vh;width: 600%;top: 0;text-align: left;margin-left: 20px;}#floor-nav .active, #floor-nav .floor-nav-item:hover {background: darkred;color: #fff;}</style>{%- if section.settings.enable_floor -%}<div id="floor-nav" style="display: none;"></div><div id="go-top"></div>{%- endif -%}{%- if section.settings.enable_floor -%}<script>$(document).ready(function() {var html = '';$('.floor-section').each(function(index, element) {var name = $(element).find('.floor-scope').attr('data-floor-name');html += `<div class="floor-nav-item"><span class="item__index">${index + 1}F</span><div class="item__name">${name}</div></div>`;})$('#floor-nav').append($(html));var floorNav = $('#floor-nav');//导航壳var floorNavItem = floorNav.find('.floor-nav-item');//导航var floorScope = $('main .floor-section');//楼层var goTopBtn = $('#go-top');//回到顶部$(window).scroll(function(){var winHeight = $(window).height();//可视窗口高度var scrollTop = $(window).scrollTop();//鼠标滚动的距离if(scrollTop>=$('#shopify-section-header').height()){floorNav.fadeIn();goTopBtn.fadeIn();//鼠标滑动式改变floorScope.each(function(index){if(winHeight + scrollTop - $(this).offset().top > winHeight/2){floorNavItem.removeClass('active');floorNavItem.eq(index).addClass('active');}})}else{floorNav.fadeOut();goTopBtn.fadeOut();}})floorNavItem.on('mouseenter', function() {var itemName = $(this).find('.item__name');itemName.fadeIn();setTimeout(() => {itemName.fadeOut();}, 1000)});//点击top回到顶部goTopBtn.click(function(){$('body,html').animate({"scrollTop":0},500)})//点击回到当前楼层floorNavItem.click(function(){var that = this;var t = floorScope.eq($(this).index()).offset().top;$('body,html').animate({"scrollTop":t}, 500, function() {$(that).siblings().removeClass('active');});$(this).addClass('active');$(this).siblings().removeClass('active');});});</script>{%- endif -%}{% schema %}{"name": "楼层开关","tag": "section","class": "floor-switch","settings": [{"type": "checkbox","id": "enable_floor","label": "是否展示楼层导航","default": false}],"presets": [{"name": "楼层开关"}]}{% endschema %}
3. 展示效果
店铺地址: https://asen-practice.myshopify.com/ 密码:test
测试链接: https://asen-practice.myshopify.com/pages/black-firday
Tips: 如有引用请标注源文章地址
关注我的【小红书】,第一时间掌握更新动态
你的鼓励就是我创作的动力!
