需求定位:导航中实现子元素滚动到父元素的最左侧

解决方案:查找该子元素的offsetLeft值,然后让父元素滚动offsetLeft,parenDom.scrollLeft = childDom.offsetLeft

在vue原型上定义scrollToIndex函数

prototype.js

/**
* 滚动到index
* @param option = {
* parentsDom //父元素 dom
* childDom //子元素 dom
* }
*/
Vue.prototype.scrollToIndex = function (options = {}) {
let width = 0;
// 在全局找到
const el = document.getElementsByClassName(options.parentsDom),
defaults = 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;
}

.vue

this.scrollToIndex({
parentsDom: 'J-nav-select',
childDom: 'J-nav-active',
});

最新文章

  1. H3 BPM:为石化企业提供一个不一样的全停大修平台
  2. Razor速记
  3. Python +crontab定时备份目录发送邮件
  4. 表格搞定 Asp.net Web 状态管理
  5. Android仿qq聊天记录长按删除功能效果
  6. SharePoint 2013 CSOM 对象模型属性包
  7. LR 解决中文乱码(来源——百度)
  8. 理解C#系列 / 核心C# / 编译参数
  9. 【Linux高频命令专题(9)】ls
  10. ocp 1Z0-042 1-60题解析
  11. jira破解
  12. apache配置虚拟主机的三种方式
  13. INI解析模块的C++实现
  14. sql错误代码一览表
  15. FPGA的SPI从机模块实现
  16. ios 渐进淡出
  17. round()函数 浮点数的四舍五入
  18. CCF CSP 201312-1 出现次数最多的数
  19. MyList 泛型委托
  20. [转] Torch中实现mini-batch RNN

热门文章

  1. spring和mybatis整合报错:org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError
  2. kafka运行找不到或无法加载主类 Files\Java\jdk1.8.0_131\lib\dt.jar;C:\Program
  3. h指数|JCR|ORCID|CCC|Research ID|BKCI|
  4. Transformer详解:各个特征维度分析推导
  5. Servlet.service() for servlet [appServlet] in context with path [/item] threw exception [Request processing failed
  6. IPFS私有网络搭建总结
  7. 解决jar包冲突
  8. 吴裕雄--天生自然python编程:pycharm常用快捷键问题
  9. 《JavaScript算法》二分查找的思路与代码实现
  10. js编写trim()函数