Простой javascript код не работает в WordPress - PullRequest
0 голосов
/ 05 апреля 2020

Мой плагин не работает. Я могу найти код js при проверке браузера и поиске кода. Кто-нибудь знает, почему функция плавной прокрутки по-прежнему не работает?

Плагин WordPress содержит 2 файла, 1 PHP файл и 1 js файл.

Код выглядит следующим образом: PHP файл: q-smooth-scroll. php

add_action ('wp_enqueue_scripts', 'test_init');

function test_init () {wp_enqueue_script ('q-smooth-scroll', plugins_url ('/js/qscroll.js', FILE ), массив ('jquery'), '', правда); }

js file: qscroll. js

// Select all links with hashes


function myQscrolFunction() {



  // Select all links with hashes
  $('a[href*="#"]')
    // Remove links that don't actually link to anything
    .not('[href="#"]')
    .not('[href="#0"]')
    .click(function(event) {
      // On-page links
      if (
        location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
        &&
        location.hostname == this.hostname
      ) {
        // Figure out element to scroll to
        var target = $(this.hash);
        target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
        // Does a scroll target exist?
        if (target.length) {
          // Only prevent default if animation is actually gonna happen
          event.preventDefault();
          $('html, body').animate({
            scrollTop: target.offset().top
          }, 1000, function() {
            // Callback after animation
            // Must change focus!
            var $target = $(target);
            $target.focus();
            if ($target.is(":focus")) { // Checking if the target was focused
              return false;
            } else {
              $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
              $target.focus(); // Set focus again
            };
          });
        }
      }
    });


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...