- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在研究 horizontal website本周并从这里的社区收到了很多建议和有用的代码(谢谢!)。
我的 JS 技能非常不稳定,但我正在更好地掌握它,我的 CodePen展示了我刚才所拥有的,以及它或多或少完美地工作。我组合了两个单独的函数来帮助 onClick 工作,但这显然对 onScroll 也不起作用。我试图了解最佳实践是什么,因为我仍然不完全理解 JS 的规则。
问题:onScroll
active
类激活正常(变成黄色),但 ion-ios-radio-button-on
没有切换到 ion-ios-radio-button-off
(反之亦然)。但是,如果您单击图标,它们将完全改变。
我试过弄乱那里的内容,例如添加更多替换/添加/删除属性,但当然它们可能不会起作用,因为我不确定将它们放在哪里。我知道这段代码也可以压缩,但我的技术水平不是那么高。
我一如既往地感谢任何有助于拓宽我知识面的建议和帮助!
// ---
const main = document.querySelector( 'main' );
const nav = document.querySelector( '.bottomnav' );
const setIconState = (icon, state) => icon.className = state ?
icon.className.replace('button-off', 'button-on') :
icon.className.replace('button-on', 'button-off')
const toggleIcon = element => {
const className = element.className;
element.className = className.indexOf('button-on') > -1 ?
setIconState(element, false) :
setIconState(element, true);
}
const setIconActiveState = (icon, state) => icon.className = state ?
icon.className = `${icon.className} active` :
icon.className = icon.className.replace('active', '')
document.querySelectorAll('.bottomnav a.icon')
.forEach(icon => {
icon.onclick = (e) => {
const {
target: clickedSpan
} = e;
const siblings = [...clickedSpan.parentElement.parentElement.querySelectorAll('a.icon')]
.filter(sibling => sibling != clickedSpan);
siblings.forEach(icon => {
setIconState(icon, false);
setIconActiveState(icon, false);
});
setIconState(clickedSpan, true);
setIconActiveState(clickedSpan, true);
};
});
let scrollend;
function onwheel(){
/* When using the scrollwheel, translate Y direction scrolls to X direction. This way scrollwheel users get the benefit of scrolling down to go right, while touch and other users get default behaviour. */
event.preventDefault();
event.stopImmediatePropagation();
main.scrollLeft += event.wheelDeltaY;
}
function onscroll(){
const current = Array.from( main.children ).find(child => {
return child.offsetLeft >= main.scrollLeft - innerWidth / 2;
});
const link = Array.from( nav.children ).reduce((find, child) => {
child.classList.remove( 'active' );
return find || (child.href.indexOf( current.id ) >= 0 ? child : find);
}, false);
if( link ) link.classList.add( 'active' );
clearTimeout( scrollend );
scrollend = setTimeout( onscrollend, 100 );
}
function onscrollend(){
/* After scrolling ends, snap the appropriate element. This could be done with an animation. */
clearTimeout( scrollend );
const current = Array.from( main.children ).find(child => {
return child.offsetLeft >= main.scrollLeft - innerWidth / 2;
});
main.scrollLeft = current.offsetLeft;
}
/* Bind and initial call */
main.addEventListener( 'wheel', onwheel );
main.addEventListener( 'scroll', onscroll );
onscroll();
html,
body,
main {
height: 100%;
scoll-behavior: smooth;
}
body {
padding: 0;
margin: 0;
}
main {
display: flex;
overflow: auto;
width: 100%;
height: 100%;
scroll-snap-type: x mandatory;
}
main section {
padding: 1%;
width: 100%;
height: 100vh;
flex: 0 0 100%;
}
.bottomnav {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.bottomnav a {
width: 1em;
height: 1em;
margin: 1em;
display: block;
overflow: hidden;
color: #888;
font-size: 26px;
/* color: transparent;
border: 1px solid black;
border-radius: 50%; */
}
.bottomnav a:hover {
color: #fac123;
transition: color .5s;
}
.bottomnav a.active {
color: #fac123;
transition: color .5s;
}
.card {
height: 100vh;
}
.cardblack {
background-color: black;
color: white;
}
<link href="https://unpkg.com/ionicons@4.5.5/dist/css/ionicons.min.css" rel="stylesheet">
<div class="bottomnav">
<a href="#1" class="icon ion-ios-radio-button-on">Section 1</a>
<a href="#2" class="icon ion-ios-radio-button-off">Section 2</a>
<a href="#3" class="icon ion-ios-radio-button-off">Section 3</a>
</div>
<main>
<section class="card cardwhite" id="1">
<h2>Section 1</h2>
</section>
<section class="card cardblack" id="2">
<h2>Section 2</h2>
</section>
<section class="card cardwhite" id="3">
<h2>Section 3</h2>
</section>
</main>
最佳答案
看起来您可以调用与点击时调用的代码相同的代码:
setIconState(link, true);
setIconActiveState(link, true);
所以你可以删除:
if (link) link.classList.add('active');
这样做有用吗?
const main = document.querySelector('main');
const nav = document.querySelector('.bottomnav');
const setIconState = (icon, state) => icon.className = state ?
icon.className.replace('button-off', 'button-on') :
icon.className.replace('button-on', 'button-off')
const toggleIcon = element => {
const className = element.className;
element.className = className.indexOf('button-on') > -1 ?
setIconState(element, false) :
setIconState(element, true);
}
const setIconActiveState = (icon, state) => icon.className = state ?
icon.className = `${icon.className} active` :
icon.className = icon.className.replace('active', '')
document.querySelectorAll('.bottomnav a.icon')
.forEach(icon => {
icon.onclick = (e) => {
const {
target: clickedSpan
} = e;
const siblings = [...clickedSpan.parentElement.parentElement.querySelectorAll('a.icon')]
.filter(sibling => sibling != clickedSpan);
siblings.forEach(icon => {
setIconState(icon, false);
setIconActiveState(icon, false);
});
setIconState(clickedSpan, true);
setIconActiveState(clickedSpan, true);
};
});
let scrollend;
function onwheel() {
event.preventDefault();
event.stopImmediatePropagation();
main.scrollLeft += event.wheelDeltaY;
}
function onscroll() {
const current = Array.from(main.children).find(child => {
return child.offsetLeft >= main.scrollLeft - innerWidth / 2;
});
const link = Array.from(nav.children).reduce((find, child) => {
child.classList.remove('active');
return find || (child.href.indexOf(current.id) >= 0 ? child : find);
}, false);
setIconState(link, true);
setIconActiveState(link, true);
clearTimeout(scrollend);
scrollend = setTimeout(onscrollend, 100);
}
function onscrollend() {
/* After scrolling ends, snap the appropriate element. This could be done with an animation. */
clearTimeout(scrollend);
const current = Array.from(main.children).find(child => {
return child.offsetLeft >= main.scrollLeft - innerWidth / 2;
});
main.scrollLeft = current.offsetLeft;
}
/* Bind and initial call */
main.addEventListener('wheel', onwheel);
main.addEventListener('scroll', onscroll);
onscroll();
html,
body,
main {
height: 100%;
scoll-behavior: smooth;
}
body {
padding: 0;
margin: 0;
}
main {
display: flex;
overflow: auto;
width: 100%;
height: 100%;
scroll-snap-type: x mandatory;
}
main section {
padding: 1%;
width: 100%;
height: 100vh;
flex: 0 0 100%;
}
.bottomnav {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.bottomnav a {
width: 1em;
height: 1em;
margin: 1em;
display: block;
overflow: hidden;
color: #888;
font-size: 26px;
/* color: transparent;
border: 1px solid black;
border-radius: 50%; */
}
.bottomnav a:hover {
color: #fac123;
transition: color .5s;
}
.bottomnav a.active {
color: #fac123;
transition: color .5s;
}
.card {
height: 100vh;
}
.cardblack {
background-color: black;
color: white;
}
<link href="https://unpkg.com/ionicons@4.5.5/dist/css/ionicons.min.css" rel="stylesheet">
<div class="bottomnav">
<a href="#1" class="icon ion-ios-radio-button-on">Section 1</a>
<a href="#2" class="icon ion-ios-radio-button-off">Section 2</a>
<a href="#3" class="icon ion-ios-radio-button-off">Section 3</a>
</div>
<main>
<section class="card cardwhite" id="1">
<h2>Section 1</h2>
</section>
<section class="card cardblack" id="2">
<h2>Section 2</h2>
</section>
<section class="card cardwhite" id="3">
<h2>Section 3</h2>
</section>
</main>
关于javascript - 替换多个类 onScroll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57292111/
使用scrolltop,我使用if条件将滚动分为5部分。在第二部分中,我给出了scrolltop值从600到700px的视频。如果我再次滚动,视频会在该位置播放多次。但我希望视频必须如果我在该值内任意
我想做的就是在页面加载时触发一个函数。 似乎我的代码中有一个错误,但我无法弄清楚...我想我还是菜鸟... Hi, function myFunction () { docume
所以我有一个元素,我希望能够使用鼠标上的滚轮放大和缩小。我认为解决这个问题的一个好方法是这样的: JavaScript: var container = document.getElementById
我想在用户滚动网页时加载更多内容。我尝试使用 onScroll 事件 javascript 被多次调用 jQuery .scroll() 方法也被多次调用。 我该如何处理这个问题?是浏览器问题吗? 注
这是简化的 html: function handle() { console.log("fired"); };
我一直在研究 horizontal website本周并从这里的社区收到了很多建议和有用的代码(谢谢!)。 我的 JS 技能非常不稳定,但我正在更好地掌握它,我的 CodePen展示了我刚才所拥有的,
我使用此代码在滚动时获取页面中元素的偏移量,但滚动函数仅执行一次并提醒 viewportWidth,然后它停止在“off”变量处 这是原始脚本中的一个简单示例,它不仅仅是将 #menuIcon 设置为
// Schließen Button Hauptnavigation $('button#collapse-button').click(function () { $('n
我知道我只能调用一次window.onscroll = function(){},因为多次调用它可能会导致它们相互覆盖。这就是为什么我问:是否可以将函数调用“推送”到 window.onscroll?
实际上我见过一个网站,在滚动时自动加载新页面并将其附加到旧页面。 不仅是页面,URL 也会在滚动时发生变化。 我完全不知道如何实现这个。这是我看过的网站 matt 。这里只要向下滚动,就会有一个无限滚
我试图在屏幕上显示相应内容期间使菜单项显示为粗体,然后在该内容滚动过去后恢复正常。 我已经尝试了很多方法来做到这一点,但它似乎并没有奏效。我可以在适当的时候让它变粗,但是当我尝试添加属性让它恢复正常时
我有一个脚本,它在文档正文上运行 onmousemove 和 onscroll 的 addEventListener 以获取光标位置。 onmousemove 事件工作正常(客户端+滚动),但是当发生
是否可以检测用户何时在 facebook Canvas 应用程序上滚动(在 iframe 中)。因此,当用户在页面上向下滚动时,我可以执行操作。 最佳答案 您可以使用 jQuery API 并尝试他们
我有一个在滚动时收缩的横幅和它下面的一些内容,我怎样才能让内容在移动前在横幅上停留几秒钟?我尝试使用 jQuery 插件 stickem,但它似乎不起作用。 JS: $(document).scrol
我有一个 JavaScript 函数,它在窗口滚动到特定点时执行不同的条件语句。 (当窗口滚动到新的部分/行时或多或少地改变了我的导航链接的样式) // variables used var pLin
hi on scroll active class 未添加到li on click on a 工作正常请看一次。 $('nav a').on('click', function() {
我是 javascript 的新手,我试图在滚动 div id="container"时触发 div id="foo",以便在滚动 id“container”时说“IM AWESOME” .我什至给了
当用户滚动超过 25 像素时,我试图将 class 添加到 id。我不确定为什么,但我放学后该功能没有启动。任何关于为什么的帮助将不胜感激。这是我的代码: window.onscoll = funct
我需要显示水平 ListView 项目的无限滚动(意味着需要重复项目)例如 a,b,c,d,e,f,a,b,c,d,e,f,a,b,.... 粗体是屏幕上可见的项目 过程是在到达最后一个列表项后重复列
我想在发生滚动事件时实现onScroll。但是我不明白如何使用我通过 onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
我是一名优秀的程序员,十分优秀!