简体   繁体   English

我的脚本不在wordpress静态站点中工作

[英]My Script is not working in wordpress static site

I am working on my wordpress site, and i use a static page for my front/home page. 我正在我的wordpress网站上工作,我在前面/主页上使用静态页面。 I wanted to use parallax scrolling but i cant get my script to load and work. 我想使用视差滚动,但我不能让我的脚本加载和工作。

I linked it in header like this: 我在标题中链接它像这样:

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/scroll.js"></script>

and this is my scroll script: 这是我的滚动脚本:

(function($) {
    $.fn.parallax = function(options) {
        var windowHeight = $(window).height();
        // Establish default settings
        var settings = $.extend({
            speed: 0.15
        }, options);

        // Iterate over each object in collection
        return this.each( function() {

        // Save a reference to the element
        var $this = $(this);

            // Set up Scroll Handler
            $(document).scroll(function(){
                var scrollTop = $(window).scrollTop();
                var offset = $this.offset().top;
                var height = $this.outerHeight();

                // Check if above or below viewport
                if (offset + height <= scrollTop || offset >= scrollTop + windowHeight) {
                return;
                }

                var yBgPosition = Math.round((offset - scrollTop) * settings.speed);

                // Apply the Y Background Position to Set the Parallax Effect
                $this.css('background-position', 'center ' + yBgPosition + 'px'); 
            });
        });
    }
}(jQuery));

$('.parallax-section-1').parallax({
speed : 0.15
});

I found some articles that i would have to use the functions.php and enque the script but i never did that before so im a bit lost, so any help would be highly appreciated. 我找到了一些文章,我将不得不使用functions.php和enque脚本,但我从来没有这样做,所以我有点迷失,所以任何帮助将非常感激。

First things first, lets load the script the "WordPress Way". 首先,让脚本加载“WordPress Way”。 This will actually help simplify troubleshooting. 这实际上有助于简化故障排除。

To do that, edit your theme's functions.php file. 为此,请编辑主题的functions.php文件。

In that file, add this PHP code. 在该文件中,添加此PHP代码。 Be sure it is between <?php opening / closing tags. 确保它在<?php开/关标签之间。

Typically, I'd recommend adding it to the end of the file, but if your file includes this ?> - that the code you add is before the ?> : 通常,我建议将它添加到文件的末尾 ,但是如果你的文件包含这个?> - 你添加的代码在?> 之前

add_action('enqueue_scripts', 'my_custom_script');

function my_custom_script() {
    // Be sure jQuery is loading
    wp_enqueue_script('jquery');
    // Load your script
    wp_enqueue_script('my-parallax-script', get_template_directory_uri() . '/js/scroll.js', array('jquery'), FALSE, TRUE);
}

Now - once you do this, you will want to be sure that your script is in fact loading. 现在 - 一旦你这样做,你将需要确保你的脚本实际上正在加载。 To do that, visit the page, then do a VIEW SOURCE in your browser. 要执行此操作,请访问该页面,然后在浏览器中执行查看源。 When you view source, do a search of the code for "scroll.js" - if you find it, great. 当您查看源代码时,请搜索“scroll.js”代码 - 如果您找到它,那就太棒了。 Click on the url (or copy / paste it into your browser window) and be sure that the script is referencing the right location. 单击URL(或将其复制/粘贴到浏览器窗口中)并确保脚本引用了正确的位置。 If not, you'll need to be sure to move the script to the right location, so the page will load it. 如果没有,您需要确保将脚本移动到正确的位置,以便页面加载它。

Next Step 下一步
You have a problem with your script that will not work in WordPress. 您的脚本有问题, 无法在WordPress中使用。 You are referencing jQuery with the $ symbol, AND it appears that you are referencing an element directly, which means there's a good chance the element doesn't exist when the script will run, which means that it won't have the desired effect. 您正在使用$符号引用jQuery,并且看起来您正在直接引用元素,这意味着当脚本运行时元素很可能不存在,这意味着它将不具有所需的效果。

Instead, we need to do two things. 相反,我们需要做两件事。 Because WordPress loads jQuery in what's called no conflict mode , you need to reference it using jQuery , not $ . 因为WordPress在所谓的无冲突模式下加载jQuery,你需要使用jQuery而不是$来引用它。 AND you need to put your code into a document ready function. 并且您需要将代码放入文档就绪函数中。

Thankfully, jQuery offers a slick way of doing the document ready that exposes the $ inside of the document ready , for convenience: 值得庆幸的是,为了方便起见,jQuery提供了一种方便的文档准备方式,可以将文档中的$ 内部公开。

// Shorthand version of "No-conflict-safe" document ready
jQuery(function($) {
    // Inside here, we can use $ instead of jQuery
    $('.parallax-section-1').parallax({
        speed : 0.15
    });
});

If it still does not work after all of this, then you need to open your console in your browser, and look for any javascript errors. 如果它在所有这些之后仍然无效,那么您需要在浏览器中打开控制台,并查找任何javascript错误。 They will help you identify what is going on, which is the first step to solving the problem. 它们将帮助您识别正在发生的事情,这是解决问题的第一步。

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

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