2012-03-20 49 views
2

例中的jsfiddle工作,我想在http://jsfiddle.net/HRewD/17/jQuery Next/Prev平滑滚动和Wordpress?

BUT..When我尝试在WordPress的落实,没有平滑滚动链接:(

我觉得它可能与$的做,所以我改变了他们所有的$ jQuery的通常工作,但这次不会。

我也使用jQuery.noConflict()的建议,但没有运气尝试。

我目前的工作在这个网站在本地,所以我目前无法在线链接到它。

我有几个其他的插件可以完美的在jQuery上调用,所以jQuery 1.6.4肯定会加载。

我意识到我没有给与很多工作,但我会很感激任何形式的领导帮助我得到这个想法。因为在这一点上我很难过。

所以如果任何人都可以伸出援助之手,我将不胜感激。


更新:

我一直在这个问题上下车,并进一步缩小了,但我仍然在努力环绕它的所有我的头。我在控制台收到一个错误,这是因为一个不可见的字符(我猜是由JSFiddle创建的!)。我在代码中的不同位置运行了一个简单的警报函数,并且显示正常。

截至目前,我没有注意到任何错误控制台或“的开发工具Safari浏览器,FF和Opera resources'section,但仅适用于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斌这方面的工作,以及一个静态的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,同时加载,加载none并希望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

截至目前,我在我的功能文件中的一个剧本,我想利用1.7.1库包括与WordPress,以避免冲突库。

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

感谢, 尼克

+2

感谢您使用JSFiddle,即使您无法链接到您的实际网站。 +1。你有没有检查你的浏览器的错误控制台,以及脚本实际上是否用'alert()'在某处执行? – Bojangles 2012-03-20 01:02:52

+0

是在控制台中,我得到了jQuery的未定义的,意外的令牌<和意外的令牌非法。我不确定这些都是特指这一个脚本,我不确定如何知道这些错误来自哪里。我也没想过看看我的代码中是否有警报返回,所以我会给它一个镜头。忍受我,我仍然在学习JavaScript。很明显,我还有一些障碍需要跳跃。 – Ncastro340 2012-03-20 01:47:29

+0

根据我的经验,jquery-tools与jquery-ui以及其他脚本命名空间相冲突,即使在使用唯一名称时也经常包含在WordPress中,请尝试删除它并使用其他库并查看。 – Wyck 2012-04-01 15:38:53

回答

0

我觉得你的平滑滚动脚本的jQuery脚本之前加载。为了确保jQuery先入队,将jquery的句柄作为依赖项添加到平滑滚动入队脚本函数中。您已经拥有了其他一些脚本,只需将其添加到平滑滚动行即可。

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

希望有所帮助。