Javascript 如何使用“上一步”和“下一步”按钮在锚链接中循环?
我有一个移动导航,主要功能是使用“上一页”和“下一页”按钮在页面中循环浏览一组锚链接 这就是我目前所能想到的逻辑:Javascript 如何使用“上一步”和“下一步”按钮在锚链接中循环?,javascript,jquery,css,Javascript,Jquery,Css,我有一个移动导航,主要功能是使用“上一页”和“下一页”按钮在页面中循环浏览一组锚链接 这就是我目前所能想到的逻辑: // Anchor links are set, these are the names const anchors = ['year-1800s', 'year-1850s', 'year-1900s', 'year-1970s']; const nextBtn = document.querySelector('.sidebar-mobile-btn-next'); const
// Anchor links are set, these are the names
const anchors = ['year-1800s', 'year-1850s', 'year-1900s', 'year-1970s'];
const nextBtn = document.querySelector('.sidebar-mobile-btn-next');
const prevBtn = document.querySelector('.sidebar-mobile-btn-prev');
function navNext() {
}
function navPrev() {
}
nextBtn.addEventListener('click', navNext());
prevBtn.addEventListener('click', navPrev());
编辑:我使用的是引导scrollspy,因此如果其中一个锚链接部分位于视口中,将有一个“活动”类。如何根据“活动”类检测要转到的锚?这可能就是您要查找的:
const-anchors=['year-1800s','year-1850s','year-1900','year-1970'];
const nextBtn=document.querySelector('.sidebar mobile btn next');
const prevBtn=document.querySelector('.sidebar mobile btn prev');
设指数=0;
功能导航下一个(e){
index=(index==anchors.length-1)?0:++索引;
e、 target.href=`${anchors[index]}`;
console.log(e.target.href,索引);
}
功能navPrev(e){
index=(index==0)?anchors.length-1:--索引;
e、 target.href=`${anchors[index]}`;
console.log(e.target.href,索引);
}
nextBtn.addEventListener(“单击”,导航下一步);
prevBtn.addEventListener(“单击”,导航预览)代码>
|
这可能就是您想要的:
const-anchors=['year-1800s','year-1850s','year-1900','year-1970'];
const nextBtn=document.querySelector('.sidebar mobile btn next');
const prevBtn=document.querySelector('.sidebar mobile btn prev');
设指数=0;
功能导航下一个(e){
index=(index==anchors.length-1)?0:++索引;
e、 target.href=`${anchors[index]}`;
console.log(e.target.href,索引);
}
功能navPrev(e){
index=(index==0)?anchors.length-1:--索引;
e、 target.href=`${anchors[index]}`;
console.log(e.target.href,索引);
}
nextBtn.addEventListener(“单击”,导航下一步);
prevBtn.addEventListener(“单击”,导航预览)代码>
|
下面是我为同一页面锚创建的解决方案。
可以相应地修改以工作,并且速度非常快。
功能页(){
var Pnum=parseFloat(document.getElementById(“MyNumber”).value);
window.location.href=“#”+Pnum;
}
第页:
&时代;
更多内容在这里。。。
更多内容在这里。。。
更多内容在这里。。。
下面是我为同一页面锚创建的解决方案。
可以相应地修改以工作,并且速度非常快。
功能页(){
var Pnum=parseFloat(document.getElementById(“MyNumber”).value);
window.location.href=“#”+Pnum;
}
第页:
&时代;
更多内容在这里。。。
更多内容在这里。。。
更多内容在这里。。。
nextBtn.addEventListener('click',navNext())代码>必须是nextBtn.addEventListener('click',navNext')
除非navNext
应该返回一个函数。将锚点的索引存储在变量中,然后单击按钮更改索引。@Mohammad谢谢,但是我如何循环锚点?如何检查当前锚点是否处于活动状态?ie.viewport正在显示#1800s部分,因此请共享您的htmlnextBtn.addEventListener('click',navNext())代码>必须是nextBtn.addEventListener('click',navNext')
除非navNext
应该返回一个函数。将锚点的索引存储在变量中,然后单击按钮更改索引。@Mohammad谢谢,但是我如何循环锚点?如何检查当前锚点是否处于活动状态?ie.viewport正在显示#1800s部分,所以请分享您的HTML这正是我要找的!非常感谢你!是的,这正是我要找的!非常感谢你!请在示例中发布与共享代码相关的代码先前的代码适用于35页网站(MyNumber.value)请在示例中发布与共享代码相关的代码先前的代码适用于35页网站(MyNumber.value
Below is a solution I created for the same page anchors.
Can be modified to work accordingly and is pretty fast.
<script>
function page() {
var Pnum = parseFloat(document.getElementById("MyNumber").value);
window.location.href="#"+Pnum;
}
</script>
Page: <em style="font-size:150%;background-color:green;border-radius:4px;position:relative;right:6px;">
<b title="Subtract 1 Here..." onclick="if (MyNumber.value>1){MyNumber.value=(parseInt(MyNumber.value))-1};page()" style="cursor:pointer"><</b>
<input onchange="page();" style="position:relative;top:-2px;right:1px;width:22px;height:20px;" value="1" alt="1" type="text" id="MyNumber" name="MyNumber" maxlength="2" size="2" min="1" max="99" >
<b title="Add 1 Here..." onclick="if (MyNumber.value<35){MyNumber.value=(parseInt(MyNumber.value))+1};page()" style="position:relative;right:4px;cursor:pointer">></b>
</em>
<b title="Reset to Current MyNumber" style="position:relative;top:-2px;right:14px;cursor:pointer;background-color:red;" onclick="MyNumber.value=MyNumber.alt;MyNumber.focus();page();return false">×</b>
<!--P1 Start --> <a name="1"></a>
More content Here...
<!--P2 Start --> <a name="2"></a>
More content Here...
<!--P3 Start --> <a name="3"></a>
More content Here...