javascript - 将 Jquery 导航功能翻译成 javascript?
问题描述
我需要将 Jquery 转换为 JavaScript 函数以获得更好的理解。它应该是一个粘性导航功能。这是代码:
类粘性导航 {
constructor() {
this.currentId = null;
this.currentTab = null;
this.tabContainerHeight = 70;
let self = this;
$('.et-hero-tab').click(function() {
self.onTabClick(event, $(this));
});
$(window).scroll(() => { this.onScroll(); });
$(window).resize(() => { this.onResize(); });
}
onTabClick(event, element) {
event.preventDefault();
let scrollTop = $(element.attr('href')).offset().top - this.tabContainerHeight + 1;
$('html, body').animate({ scrollTop: scrollTop }, 600);
}
onScroll() {
this.checkTabContainerPosition();
this.findCurrentTabSelector();
}
onResize() {
if(this.currentId) {
this.setSliderCss();
}
}
checkTabContainerPosition() {
let offset = $('.et-hero-tabs').offset().top + $('.et-hero-tabs').height() - this.tabContainerHeight;
if($(window).scrollTop() > offset) {
$('.et-hero-tabs-container').addClass('et-hero-tabs-container--top');
}
else {
$('.et-hero-tabs-container').removeClass('et-hero-tabs-container--top');
}
}
findCurrentTabSelector(element) {
let newCurrentId;
let newCurrentTab;
let self = this;
$('.et-hero-tab').each(function() {
let id = $(this).attr('href');
let offsetTop = $(id).offset().top - self.tabContainerHeight;
let offsetBottom = $(id).offset().top + $(id).height() - self.tabContainerHeight;
if($(window).scrollTop() > offsetTop && $(window).scrollTop() < offsetBottom) {
newCurrentId = id;
newCurrentTab = $(this);
}
});
if(this.currentId != newCurrentId || this.currentId === null) {
this.currentId = newCurrentId;
this.currentTab = newCurrentTab;
this.setSliderCss();
}
}
setSliderCss() {
let width = 0;
let left = 0;
if(this.currentTab) {
width = this.currentTab.css('width');
left = this.currentTab.offset().left;
}
$('.et-hero-tab-slider').css('width', width);
$('.et-hero-tab-slider').css('left', left);
}
}
新的 StickyNavigation();
我试着翻译它,但是我一直失败,我想我对 JavaScript 的了解还不是很好(我是 vanilla javascript 的初学者)。我将不胜感激!
解决方案
推荐阅读
- wordpress - Wordpress 在具有任何类型的永久链接的任何页面中都提供 404,但是当我更改主题时它可以工作。怎么修?
- machine-learning - DNN二元分类器的准确率没有增加
- python - 在 Django 视图中显示表格和表单
- javascript - 为什么我的 JavaScript 事件会在任何输入上执行,而不是输入到 Chrome 中的正确字段?
- visual-studio-code - 如何在提交 VS Code 时停止自动格式化
- javascript - 如何在javascript中获取函数MULTIPLO.SUPERIOR的结果?
- webfocus - 如何在 Webfocus 中排除数据集中的某些组合模式?
- angular - 避免在状态更新时重新加载图像
- angular - Alfresco 与 Angular 7 的集成
- angular - Angular 7/ RxJs:轮询端点并响应第一个端点