工具之scroolToIndex
2024-10-08 22:33:28
需求定位:导航中实现子元素滚动到父元素的最左侧
解决方案:查找该子元素的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',
});
最新文章
- H3 BPM:为石化企业提供一个不一样的全停大修平台
- Razor速记
- Python +crontab定时备份目录发送邮件
- 表格搞定 Asp.net Web 状态管理
- Android仿qq聊天记录长按删除功能效果
- SharePoint 2013 CSOM 对象模型属性包
- LR 解决中文乱码(来源——百度)
- 理解C#系列 / 核心C# / 编译参数
- 【Linux高频命令专题(9)】ls
- ocp 1Z0-042 1-60题解析
- jira破解
- apache配置虚拟主机的三种方式
- INI解析模块的C++实现
- sql错误代码一览表
- FPGA的SPI从机模块实现
- ios 渐进淡出
- round()函数 浮点数的四舍五入
- CCF CSP 201312-1 出现次数最多的数
- MyList 泛型委托
- [转] Torch中实现mini-batch RNN
热门文章
- spring和mybatis整合报错:org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError
- kafka运行找不到或无法加载主类 Files\Java\jdk1.8.0_131\lib\dt.jar;C:\Program
- h指数|JCR|ORCID|CCC|Research ID|BKCI|
- Transformer详解:各个特征维度分析推导
- Servlet.service() for servlet [appServlet] in context with path [/item] threw exception [Request processing failed
- IPFS私有网络搭建总结
- 解决jar包冲突
- 吴裕雄--天生自然python编程:pycharm常用快捷键问题
- 《JavaScript算法》二分查找的思路与代码实现
- js编写trim()函数