1

我只是有以下脚本,如果您查看$('html,body').animate({第 9 行,您会看到- 问题是,当屏幕宽度低于 1030px 时scrollTop: target.offset().top - 95 }, 700);,我将如何更改数字95 (在 offset().top XX 之后)?

我试图添加这行代码并尝试解决它,但它不起作用 - 我不知道我做错了什么。if (width <= 1030) {

这是脚本:

//Menu Scrolling To Sections//
    $(document).ready(function () {
    $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        || location.hostname == this.hostname) {

        var target = $(this.hash);
        target = target.length ? target : $('[name=' + this.hash.slice(1) +']');

    if (target.length) {
        $('html,body').animate({
        scrollTop: target.offset().top - 95 }, 700);
    return false;
    }}});});
4

1 回答 1

0

解决方案

你需要改变:

if (target.length) {
    $('html,body').animate({
    scrollTop: target.offset().top - 95 }, 700);

至:

if (target.length) {
    var topPadding= 0;
    if($(window).width() >= 1030)
        topPadding = 95;
    $('html,body').animate({
    scrollTop: target.offset().top - topPadding }, 700);

解释

var topPadding= 0; //Sets the default padding to 0 (for when the window is below 1300px)
if($(window).width() >= 1030) //Checks to see if the window width is wider than 1300px
    topPadding = 95; //If it is then set's padding to 95px as in your code above

注意:我改变了你必须95去的topPadding地方(你设置的地方scrollTop

于 2013-09-25T17:22:59.387 回答