Most left navigation scroll achieve sub-element to the parent element: Positioning demand
Solution: Find offsetLeft value of the child element, and then let the parent element rolling offsetLeft, parenDom.scrollLeft = childDom.offsetLeft
ScrollToIndex function defined on the prototype vue
prototype.js
/ * * * Scroll to the index * @param {Option = * // parent element parentsDom DOM * childDom // child element DOM *} * / Vue.prototype.scrollToIndex = function (Options = {}) { the let width = 0 ;
found in the global //
const EL = document.getElementsByClassName (options.parentsDom), Defaults = the setDefault (Options), elChild = document.getElementsByClassName (options.childDom); IF (! judgeOptions (Defaults)) { return ; } IF (Defaults .x) { width= elChild[0].offsetLeft; } scrollLeft(el, width); }; function setDefault(options) { const defaults = { parentsDom: '', childDom: '', x: true, y: false, }; return Object.assign({}, defaults, options); } function judgeOptions(options) { console.log(options, 'options'); if (typeof options.parentsDom !== 'string' || typeof options.childDom !== 'string' || document.getElementsByClassName(options.parentsDom).length === 0 || document.getElementsByClassName(options.childDom).length === 0 ) { console.warn('Dom必须传是className并且存在'); return false; } return true; } function scrollLeft(el = '', width = 0) { if (!el) { return; } el[0].scrollLeft = width; }
.view
this.scrollToIndex({ parentsDom: 'J-nav-select', childDom: 'J-nav-active', });