简体   繁体   English

滚动到div,然后点击链接

[英]Scroll to div on link click

There are questions all over SO about this topic, but I've tried a ton of different scripts for this and I cannot get it to work with my site. 到处都有关于这个主题的问题,但是我为此尝试了许多不同的脚本,但是我无法使其与我的网站一起使用。

I am building a personal portfolio wordpress theme for myself, and would like to keep it a one page theme. 我正在为自己建立一个个人投资组合wordpress主题,并希望将其保持为一页主题。 What I want is when a user clicks a link on the navigation, the page scrolls down to that section. 我想要的是当用户单击导航上的链接时,页面向下滚动到该部分。 Easy right? 容易吧? No. 没有。

I don't know why it isn't working on my site, but I think it has to do with the script I am using for the scroll to fixed navigation. 我不知道为什么它不能在我的网站上运行,但是我认为这与我用于滚动到固定导航的脚本有关。

Here is the script I am currently attempting to use to create this in-page navigation scrolling effect: http://css-tricks.com/snippets/jquery/smooth-scrolling/ 这是我当前试图用来创建此页内导航滚动效果的脚本: http : //css-tricks.com/snippets/jquery/smooth-scrolling/

And here is the script I am using to create the scroll to fixed navigation effect: 这是我用来创建滚动到固定导航效果的脚本:

window.onscroll=function () {
    var top = window.pageXOffset ? window.pageXOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
    if(top > 640){
        document.getElementById("nav").style.position = "fixed";
        document.getElementById("nav").style.height="65px";
    } else {
        document.getElementById("nav").style.position = "relative";
        document.getElementById("nav").style.height="65px";
    }
}

You can view the site I am trying to do this on at http://www.tylerb.me 您可以在http://www.tylerb.me上查看我正在尝试执行的网站

Are the two scripts contradicting each other and making one of them not work? 这两个脚本是否相互矛盾,使其中之一不起作用?

It would appear wordpress uses the jQuery.noConflict() method to prevent well.. conflicts from occurring. 看来wordpress使用jQuery.noConflict()方法来防止发生冲突。 Because of the script conflicts(moo tools uses $ as well, for example), replace each $ with jQuery . 由于脚本冲突(例如,moo工具也使用$),请将每个$替换$ jQuery This should fix your issue.. and I do mean in the code which calls the plugin, not the plugin itself. 这应该可以解决您的问题..我的意思是在调用插件的代码中,而不是插件本身。

Example: 例:

//normal jquery method call:
$("element").method(etc);

//with noConflict code:
jQuery("element").method(etc);

UPDATE 1: 更新1:

It would appear that you forgot to leave a particular $ alone. 看来您忘记了留下特定的$ This line: 这行:

var target = jQuery(this.hash), target = this.hash;

Should be this: 应该是这样的:

var $target = jQuery(this.hash), target = this.hash;

This of course goes with any variable that begins with a $ . 当然,这与以$开头的任何变量都匹配。 Those ones should not be deleted. 这些不应该被删除。

The core of your issue is that jQuery is not loading. 您问题的核心是jQuery无法加载。

在此处输入图片说明

Daedalus just got there before me. 达达洛斯刚到我那里。 It's no conflict that's causing your issue. 不会引起您的问题的冲突。 Just use jQuery rather than $. 只需使用jQuery而不是$。

This could prove useful to some. 这可能对某些人有用。

Try using: 尝试使用:

jQuery(document).ready(function($){
    // do stuff here using $
});

instead of 代替

$(document).ready(function($){
    // do stuff here
});

This declares a local $ instead of conflicting with the previously declared. 这将声明一个本地$而不是与先前声明的冲突。

It helped me lots of times. 它帮助了我很多次。

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

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