jquery - jQuery Next/Prev平滑滚动和Wordpress?

标签 jquery wordpress smooth-scrolling

jsFiddle中的示例可以在http://jsfiddle.net/HRewD/17/中正常运行

但是..当我尝试在wordpress中实现时,没有平滑的滚动链接:(

我觉得可能与$有关,因此我将它们全部从$更改为通常可以正常工作的jQuery,但这次不行。

我还尝试按照建议使用jQuery.noConflict(),但是没有运气。

我目前正在本地使用此网站,因此目前无法在线链接到该网站。

我还有其他一些可以在jQuery上正常运行的插件,因此jQuery 1.6.4肯定正在加载。

我意识到我并没有付出太多努力,但是我非常感谢任何可以帮助我弄清楚这个问题的线索。因为在这一点上我很沮丧。

因此,如果有人可以伸出援手,我将不胜感激。



更新:

我一直在研究和解决这个问题,并进一步缩小了范围,但我仍在努力将所有问题包裹住。我在控制台中收到一个错误,这是因为一个不可见的字符(我猜是由JSFiddle创建的?!)。我在代码中的各个地方运行了一个简单的警报功能,它显示得很好。

到目前为止,我还没有注意到野生动物园,FF和Opera开发人员工具的控制台或“资源”部分中的任何错误,但仅在Chrome中,我得到了错误

event.layerX and event.layerY are broken and deprecated in WebKit. They will be removed from the engine in the near future.

仅在单击下一个或上一个错误错误左侧的数字后,才出现此错误,该数字在每次单击后以2的增量递增。

我的html:

<div id="home-block">
    <div class="current">Content Goes here</div>
    <div>A box of content</div>
    <div>Content</div>
    <div>More content...</div>
</div>

<div id="nav-right">
    <a href="#" id="prev">Previous</a>
    <a href="#" id="next">Next</a>
</div>


我的CSS:

#home-block div{
width: 300px;
height: 400px;
border: 1px solid #000;
box-shadow: 1px 1px 3px #888;
margin: 10px 10px 10px 15px;
}

.post-contain{
position: relative;
margin: 0 auto;
width: 450px;
border: 1px solid #000;
}

#nav-right{
    position: fixed;
    right: 15px;
    top: 35%;
}

.current {
    color: red;
}


我的Js:

<script type="text/javascript">

$jq(document).ready(function($jq) {
    var scrollTo = function(yoza) {
        $jq('html, body').animate({
            scrollTop: $jq(yoza).offset().top
       }, 300);
    };

    $jq('#next').click(function(event) {
        event.preventDefault();
        var $jqcurrent = $jq('#home-block > .current');
        if ($jqcurrent.index() != $jq('#home-block > div').length - 1) {
            $jqcurrent.removeClass('current').next().addClass('current');
            scrollTo($jqcurrent.next());
        }
    });
    $jq('#prev').click(function(event) {
        event.preventDefault();
        var $jqcurrent = $jq('#home-block > .current');
        if (!$jqcurrent.index() == 0) {
           $jqcurrent.removeClass('current').prev().addClass('current');
            scrollTo($jqcurrent.prev());
        }
    });
});

</script>


我的脚本在functions.php(wordpress)中被调用:

function my_scripts_method() {


wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');
wp_enqueue_script( 'jquery' );

wp_register_script('smooth-scroll', get_bloginfo('stylesheet_directory').'/js/jquery.scrollTo-1.4.2-min.js', false);
wp_enqueue_script('smooth-scroll');

wp_register_script('tabbedcontent', get_bloginfo('stylesheet_directory').'/js/tabbedContent.js', array('jquery'), '1.0', false);
wp_enqueue_script('tabbedcontent');

wp_register_script('jquery-tools', ("http://cdn.jquerytools.org/1.2.6/jquery.tools.min.js"), false);
wp_enqueue_script('jquery-tools');

wp_register_script('dock-menu', get_bloginfo('stylesheet_directory').'/js/dock-interface.js', array('jquery'), false);
wp_enqueue_script('dock-menu');

    }
add_action('wp_enqueue_scripts', 'my_scripts_method');


Here is a working example in JS Fiddle as I provided earlier

我也可以在JS Bin和静态html页面中使用此功能,但不能在wordpress中使用。

我尝试过的事情:

我确保Jquery库实际上是通过以下功能加载的:

<script type="text/javascript">
if (typeof jQuery != 'undefined') {  
    // jQuery is loaded => print the version
    alert(jQuery.fn.jquery);
}
</script>


返回1.6.4。我发现此版本似乎来自jquery工具脚本。

我已停用所有其他插件,以防某些插件可能正在加载自己的jQuery库。
我已经隔离了要在我的functions.php文件中加载的脚本:我试图取消注册wordpress'包含的JQ,并且仅加载1.6.4,仅加载1.7.1,同时加载,不加载并且希望wordpress'包含的JQ库将不仅可以解决问题,还可以重新安排它们的加载顺序,但是这些都不给我任何有关此问题的见解。

我已经测试了变量是否存在,并且可以从前面提到的JS脚本中获得这些变量,并且返回的脚本确认变量的存在。

我尝试使用$符号玩游戏:

var $jq = jQuery.noConflict(); 


并将所有实例更改为$ jq以及jQuery,但均不执行任何操作。

我还尝试过更改#next,#prev和.post变量的名称,以为它们可能与具有相同名称但没有其他名称的其他变量发生冲突。

我检查了文件路径,阅读了许多论坛主题

其他值得一提的事情:

我还有其他来自这些相同库的jQuery模块吗?
没有控制台错误?
这可能没有用,但我注意到的一件事是,单击上一个或下一个之后,我的屏幕将伸展,从而页面右侧的滚动条现在被隐藏,但立即重新出现。

这可能已被杀死,但我认为提供源可能是一个好主意(仍在本地工作并且无法链接到该站点):

<!DOCTYPE html>
<html dir="ltr" lang="en-US">

<head>

<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width">
<title>scroll 2 | </title>



<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="stylesheet" type="text/css" media="all" href="http://localhost:8888/wp-content/themes/Twentyten-templates/style.css" />
<link rel="stylesheet" type="text/css" media="all" href="http://localhost:8888/wp-content/themes/Twentyten-templates/css/post.css" />
<link rel="stylesheet" type="text/css" media="all" href="http://localhost:8888/wp-content/themes/Twentyten-templates/css/tabbedContent.css" />
<link rel="stylesheet" type="text/css" media="all" href="http://fonts.googleapis.com/css?family=Droid+Sans|Droid+Sans+Mono" />
<link rel="stylesheet" type="text/css" media="all" href="http://localhost:8888/wp-content/themes/Twentyten-templates/css/scrollable-gallery.css" />
<link rel="stylesheet" type="text/css" media="all" href="http://localhost:8888/wp-content/themes/Twentyten-templates/css/the-system.css" />
<link rel="stylesheet" type="text/css" media="all" href="http://localhost:8888/wp-content/themes/Twentyten-templates/css/dock.css" />
<link rel="stylesheet" type="text/css" media="all" href="http://localhost:8888/wp-content/themes/Twentyten-templates/css/system-hover-grid.css" />





<link rel="pingback" href="http://localhost:8888/xmlrpc.php" />



<link rel="alternate" type="application/rss+xml" title=" &raquo; Feed" href="http://localhost:8888/feed/" />
<link rel="alternate" type="application/rss+xml" title=" &raquo; Comments Feed" href="http://localhost:8888/comments/feed/" />
<script type='text/javascript' src='http://localhost:8888/wp-includes/js/comment-reply.js?ver=20090102'></script>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js?ver=3.3.1'></script>
<script type='text/javascript' src='http://localhost:8888/wp-content/themes/Twentyten-templates/js/jquery.scrollTo-1.4.2-min.js?ver=3.3.1'></script>
<script type='text/javascript' src='http://localhost:8888/wp-content/themes/Twentyten-templates/js/tabbedContent.js?ver=1.0'></script>
<script type='text/javascript' src='http://cdn.jquerytools.org/1.2.6/jquery.tools.min.js?ver=3.3.1'></script>
<script type='text/javascript' src='http://localhost:8888/wp-content/themes/Twentyten-templates/js/dock-interface.js?ver=3.3.1'></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://localhost:8888/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://localhost:8888/wp-includes/wlwmanifest.xml" /> 
<link rel='prev' title='scrollTo' href='http://localhost:8888/scrollto/' />
<meta name="generator" content="WordPress 3.3.1" />
<link rel='canonical' href='http://localhost:8888/scroll-2/' />
<style type="text/css">
body.custom-background { background-color: #fff; }
</style>




</head>







<div id="wrapper" class="hfeed">

    <div id="header">

        <div id="masthead">

    <div id="pagemenu" role="navigation">
            </div>

            <div id="branding" role="banner">
                            <div id="site-title">
                    <span>
                        <a href="http://localhost:8888/" title="" rel="home"></a>
                    </span>
                </div>
                <div id="site-description"></div>



                        <img src="http://americandreamenergysystem.com/wp-content/uploads/2010/10/ADES-Header1.png" width="0" height="0" alt="" />

            </div><!-- #branding -->
<nav class="top">
<div class="nav-contain">
<ul class="left">
<li><a href="http://localhost:8888/">Home</a></li> 
<li> <a href="http://localhost:8888/who-we-are/">About Us</a> </li> 
<li><a href="http://localhost:8888:your-system/ ‎">Your System</a></li> 
</ul>
<ul class="right">
<li> <a href="http://localhost:8888/pyp/">Your Plan</a> </li> 
<li><a href="http://localhost:8888/faq//">FAQ</a></li> 
<li><a href="http://localhost:8888/customer-service/">Contact Us</a></li> 
</ul> 
</div>


</nav>
<div id="nav-accent"></div>

            </div><!-- #access -->
        </div><!-- #masthead -->
    </div><!-- #header -->

<img src="http://localhost:8888/wp-content/themes/Twentyten-templates/images/big-circle-v7.png" id="round-overlay">


    <div id="main">
<body class="page page-id-3259 page-template page-template-scroll2-php logged-in custom-background">

</div>






<style>
p.copy {
display:none;
}

#colophon {
    border-top: 0px solid #000000;
    }

 article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }


#home-block div{
width: 300px;
height: 400px;
border: 1px solid #000;
box-shadow: 1px 1px 3px #888;
margin: 10px 10px 10px 15px;
}

.post-contain{
position: relative;
margin: 0 auto;
width: 450px;
border: 1px solid #000;
}

#nav-right{
    position: fixed;
    right: 15px;
    top: 35%;
}

.current {
    color: red;
}

.temp-box {
width: 100%;
height: 100px;
display: block;
}

</style>



<div id="AD-Logo"></div>
<div class="temp-box">spacer</div>
<div id="home-block">
    <div class="current">Content Goes here</div>
    <div>A box of content</div>
    <div>Content</div>
    <div>More content...</div>
</div>

<div id="nav-right">
    <a href="#" id="prev">Previous</a>
    <a href="#" id="next">Next</a>
</div>



        <div id="container" class="one-column">


            <div id="content" role="main">


                <div id="post-3259" class="post-3259 page type-page status-publish hentry">
                    <h1 class="entry-title">scroll 2</h1>
                    <div class="entry-content">
                        <p>a great sailor was not made by smooth waters..</p>
                                                <span class="edit-link"><a class="post-edit-link" href="http://localhost:8888/wp-admin/post.php?post=3259&amp;action=edit" title="Edit Page">Edit</a></span>                    </div><!-- .entry-content -->
                </div><!-- #post-## -->




            </div><!-- #content -->
        </div><!-- #container -->

    </div><!-- #main -->


    <div id="footer" role="contentinfo">




        <div id="colophon">

<div id="copy-foot">
<p class="copy">Copyright &copy; All rights reserved 2012</p>
</div>
<div id="negatron"></div>




            <div id="footer-widget-area" role="complementary">

                <div id="first" class="widget-area">
                    <ul class="xoxo">
                        <li id="text-5">            <div class="textwidget"><a id="footer-text"  href="http://americandreamenergysystem.com/">Home</a><br/>
<a id="footer-text"  href="http://americandreamenergysystem.com/?page_id=276">Who We Are</a></div>
        </li>                   </ul>
                </div><!-- #first .widget-area -->

                <div id="second" class="widget-area">
                    <ul class="xoxo">
                        <li id="text-21">           <div class="textwidget"><a id="footer-text"  href="http://americandreamenergysystem.com/?page_id=895">How We Do It<br/>
<a id="footer-text"  href="http://americandreamenergysystem.com/?page_id=46">Your Energy System</a></div>
        </li>                   </ul>
                </div><!-- #second .widget-area -->

                <div id="third" class="widget-area">
                    <ul class="xoxo">
                        <li id="text-22">           <div class="textwidget"><a id="footer-text" href="http://americandreamenergysystem.com/?page_id=184">Pick Your Plan</a><br/>
<a id="footer-text" href="http://americandreamenergysystem.com/?page_id=442">Home Additions</a></div>
        </li>                   </ul>
                </div><!-- #third .widget-area -->

                <div id="fourth" class="widget-area">
                    <ul class="xoxo">
                        <li id="text-23">           <div class="textwidget">
<a id="footer-text" href="http://americandreamenergysystem.com/?page_id=50">Contact Us</a><br/>
</div>
        </li>                   </ul>
                </div><!-- #fourth .widget-area -->

            </div><!-- #footer-widget-area -->

            <div id="site-info">
                <a href="http://localhost:8888/" title="" rel="home">
                                    </a>
            </div><!-- #site-info -->

            <div id="site-generator">

            </div><!-- #site-generator -->

            </div><!-- #colophon -->

         <!-- #copyright -->


    </div><!-- #footer -->

</div><!-- #wrapper -->


<div id="foot-note">

</div>

<script type="text/javascript">
if (typeof jQuery != 'undefined') {  
    // jQuery is loaded => print the version
    alert(jQuery.fn.jquery);
}
</script>



<script>

jQuery(".scrollable").scrollable();

jQuery(".items img").click(function() {

    // see if same thumb is being clicked
    if (jQuery(this).hasClass("active")) { return; }

    // in the above example replace the url assignment with this line
    var url = jQuery(this).attr("alt");

    // get handle to element that wraps the image and make it semi-transparent
    var wrap = jQuery("#image_wrap").fadeTo("medium", 1);

    // the large image from www.flickr.com
    var img = new Image();


    // call this function after it's loaded
    img.onload = function() {

        // make wrapper fully visible
        wrap.fadeTo("fast", 1);

        // change the image
        wrap.find("img").attr("src", url);

    };

    // begin loading the image from www.flickr.com
    img.src = url;

    // activate item
    jQuery(".items img").removeClass("active");
    jQuery(this).addClass("active");

// when page loads simulate a "click" on the first image
}).filter(":first").click();

</script>



<script type="text/javascript">

    jQuery(document).ready(
        function()
        {
            jQuery('#dock').Fisheye(
                {
                    maxWidth: 50,
                    items: 'a',
                    itemsText: 'span',
                    container: '.dock-container',
                    itemWidth: 40,
                    proximity: 90,
                    halign : 'center'
                }
            )
        }
    );

</script>

<script type="text/javascript">

     var $jq = jQuery.noConflict();  
    $jq(document).ready(
        function()
        {
            $jq('#dock').Fisheye(
                {
                    maxWidth: 50,
                    items: 'a',
                    itemsText: 'span',
                    container: '.dock-container',
                    itemWidth: 55,
                    proximity: 60,
                    halign : 'center'
                }
            )

            $jq('#dock2').Fisheye(
                {
                    maxWidth: 60,
                    items: 'a',
                    itemsText: 'span',
                    container: '.dock-container2',
                    itemWidth: 55,
                    proximity: 80,
                    alignment : 'left',
                    valign: 'bottom',
                    halign : 'center'
                }
            )   
        }
    );

</script>

<script type="text/avascript">

var $jq = jQuery.noConflict();  

</script>

<script type="text/javascript">

$jq(document).ready(function($jq) {
    var scrollTo = function(yoza) {
        $jq('html, body').animate({
            scrollTop: $jq(yoza).offset().top
       }, 300);
    };

    $jq('#next').click(function(event) {
        event.preventDefault();
        var $jqcurrent = $jq('#home-block > .current');
        if ($jqcurrent.index() != $jq('#home-block > div').length - 1) {
            $jqcurrent.removeClass('current').next().addClass('current');
            scrollTo($jqcurrent.next());
        }
    });
    $jq('#prev').click(function(event) {
        event.preventDefault();
        var $jqcurrent = $jq('#home-block > .current');
        if (!$jqcurrent.index() == 0) {
           $jqcurrent.removeClass('current').prev().addClass('current');
            scrollTo($jqcurrent.prev());
        }
    });
});

</script>    


</body>
</html>


在这一点上,我不知道还有什么尝试。显然,有些事情不整齐,但对我来说并不明显。我认为这很简单,而且这种方式使我更加复杂。我真的可以帮忙解决这个问题。

因此,如果有人注意到我在做什么错,请指出我思考过程中的错误,因为我不知道这种冲突的根源。

谢谢,
缺口

更新:

从功能文件中删除jQuery工具和1.7.1仍不能解决问题。

我继续上传网站,以简化此过程。

Here

到目前为止,我的函数文件中有一个脚本,并且我试图利用wordpress附带的1.7.1库来避免库冲突。

因此,也许现在我正在处理的页面可见,这将更容易缩小范围。

谢谢,
缺口

最佳答案

我认为您的平滑滚动脚本要比jquery脚本先加载。为了确保首先将jQuery排队,请将“ jquery”句柄作为依赖项添加到平滑滚动排队脚本功能。您已经在其他一些脚本中拥有了此功能,只需将其添加到平滑滚动行中。

wp_register_script('smooth-scroll', get_bloginfo('stylesheet_directory').'/js/jquery.scrollTo-1.4.2-min.js', array('jquery');


希望能有所帮助。

关于jquery - jQuery Next/Prev平滑滚动和Wordpress?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780047/

相关文章:

jquery - 使用ajax显示数据库中的图像

php - bash 脚本输出不显示在网页上

php - 我的函数需要什么参数来回显 get_results() 查询结果

jQuery 滚动插件

ios - 由于 NSAttributedString,UITableView 滚动不流畅

jquery - 使用 .each() 函数不允许我使用 .on() 函数

jquery - 删除一个正则表达式中的 2 个(多个)匹配类

php - 如何使用 wordpress 的 do_shortcode 和用户输入的值格式化此 echo 语句?

wordpress - 如何从 Woocommerce 订单中获取每个包裹的运费?

android - Horizo​​ntalScrollView smoothScrolTo 不起作用