简体   繁体   English

单页网站导航所选状态

[英]Single page website navigation selected states

New to SO! SO的新手! Please suggest a better way to phrase this question :) 请提出一个更好的方式来表达这个问题:)

I have a single page, vertical scrolling website with a fixed navigation bar located in the footer. 我有一个单页的垂直滚动网站,页脚中有一个固定的导航栏。 I am using local.scroll and anchors to link the navigation to divs on the page. 我正在使用local.scroll和anchors将导航链接到页面上的div。

I would like the user to be able to click on one link and have it change to a selected state. 我希望用户能够单击一个链接并将其更改为选定状态。 I'm just not sure how to style/code the links for a single page (instead of using class="select" for each active link as in multiple page sites.) 我只是不确定如何对单个页面的链接进行样式/编码(而不是像在多个页面站点中那样,对每个活动链接使用class =“ select”。)

This site has an example of what I'd like to accomplish: http://www.kristaganelon.com/#portfolio-section 该站点提供了我要完成的示例: http : //www.kristaganelon.com/#portfolio-section

The example you referenced is using Javascript (in the form of the popular library JQuery) to create the selected state on the navigation as well as scroll the page. 您引用的示例使用Javascript(以流行的库JQuery的形式)在导航上创建选定状态以及滚动页面。

JQuery jQuery查询

This library is very popular for creating the kind of event handlers you are looking for, as it easily binds events using simple CSS selectors familiar to anyone who has styled a page with CSS. 该库在创建所需类型的事件处理程序方面非常受欢迎,因为它可以使用熟悉CSS样式页面的人熟悉的简单CSS选择器轻松绑定事件。 The conventions of IDs, classes and attributes are used to find elements and bind events or change their states. ID,类和属性的约定用于查找元素并绑定事件或更改其状态。

The JQuery site has plenty of helpful tutorials, but a simple click event looks something like this: JQuery站点上有很多有用的教程,但是一个简单的click事件看起来像这样:

    $(document).ready(function(){
      $('.button').click(function(){
        alert('You clicked me!');
    });
});

You could utilize JQuery's .addClass to make clicking the element give it certain visual state: 您可以利用JQuery的.addClass来使元素单击以使其具有一定的视觉状态:

$(document).ready(function(){
      $('.button').click(function(){
        $(this).addClass('clicked');
    });
});

I suggest you look over the libraries documentation and learn how to first include it on your page, and then look over some basic event binding, toggling (since you'll need this to remove the active state), etc. Learning these things will help you create interactive elements much easier. 建议您查看库文档,并学习如何首先将其包含在页面中,然后查看一些基本的事件绑定,切换(因为您需要使用它来删除活动状态),等等。学习这些内容将有所帮助您可以轻松创建交互式元素。

Have a look at JQuerys .toggleClass(classname) . 看看JQuerys .toggleClass(classname) You could provide a fallback via css' pseudo-classes (not equal to the javascript solution since it is temporary). 您可以通过CSS的伪类提供后备广告(由于它是临时的,因此不等于JavaScript解决方案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM