【发布时间】:2016-08-14 19:04:55
【问题描述】:
我正在尝试使用 jQuery 中的 PHP 代码在同一页面上实现平滑滚动(尝试使用 PHP 生成最终的 jquery 代码)。但由于某种原因它不起作用,如果有人能指出错误或为什么它不起作用,我将不胜感激
// When the Document Object Model is ready
jQuery(document).ready(function(){
// 'scroll' is the amount of pixels destination
// is from the top of the document
var scroll = jQuery('<?php echo get_option('smooth_scroll'); ?>').offset().top;
// When button is clicked
jQuery('.test').click(function(){
// Scroll down to 'scroll'
jQuery('html, body').animate({scrollTop:scroll}, 'slow');
// Stop the link from acting like a normal anchor link
return false;
});
});
我有一个带有“test”类的href链接。单击该按钮时,我希望它平滑滚动以转到同一页面的特定 div[通过 get_option('smooth_scroll')] div 将由用户更新)。
【问题讨论】:
-
一般来说,您应该避免在 Javascript 或 jQuery 中模板化 PHP,但这是一种意见。但是,为了让任何人帮助您解决问题,您需要提供 get_option('smooth_scroll) 输出的值。您还应该查看浏览器的控制台并添加您可能遇到的任何错误。
-
在“.offset().top;”上出现“scrolltop.js:82 Uncaught SyntaxError: missing ) after argument list”错误行