如何避免带查询字符串的URL打开Elementor切换项时自动向下滚动
解决方案
你代码中的自动滚动行为由显式定义的页面滚动动画逻辑导致,直接移除这段逻辑即可实现「仅打开对应toggle标签、页面加载时保持在顶部」的需求,修改后的完整代码如下:
<script> document.addEventListener('DOMContentLoaded', function() { setTimeout(function() { jQuery(function($){ let toggletitles = $('.elementor-toggle-item .elementor-tab-title'); let strings = [ '?technical', '?configuration', '?safety', '?cycle-life', '?discharge', '?charging', '?environmental', '?mechanical' ]; strings.forEach( (string,i) => { if (window.location.href.indexOf(string) > -1) { toggletitles.eq(i).click(); // 已删除原有的自动滚动动画代码 } }); }); }, 1200); }); </script>
如果后续需要在用户主动交互(如点击按钮、手动滚动到指定区域)时再滚动到对应标签位置,只需将删除的滚动动画代码绑定到对应的用户交互事件上即可。
内容的提问来源于stack exchange,提问作者Caleb Hager
相关产品推荐
相关产品推荐

