0

这个项目是一个网站,它的所有页面/部分都在一个页面中。我已经为每个部分(sectio1、section2、section3 等)分配了 ID,并且我还在顶部导航 href 中分配了这些部分,例如<a href="**#section2**">**About us**</a>. 因此,当您从顶部导航单击 About us 时,它将带您到 ID 为 的 Div #section2

我做对了这部分,但是我遇到了一些问题 - 我已经研究过,但我认为我不知道它的实际关键字来找到解决方案!所以我什么也没找到,任何帮助将不胜感激。

1)当点击菜单中的按钮时,它会跳转到指定的部分,但它实际上并没有跳转到该部分的最顶部 - 它就像向下滚动到 100px 一样?!为什么它会这样做……

2)有没有办法让它平滑滚动而不跳转?

3)是否有一种简单的方法可以使按钮在单击并滚动到其部分时具有 .active 类?

这里是小提琴: http: //jsfiddle.net/nd8MN/3/这里是 HTML & CSS;

<head>
    <meta charset="utf-8" />

    <title>Spacehopper Design</title>
    <meta name="description" content=""/>
    <meta name="keywords" content="">

    <meta name="viewport" id="viewport" content="width=device-width, minimum-scale=1.0, user-scalable=0, maximum-scale=10.0, initial-scale=1.0" />      

    <link rel="shortcut icon" href="/assets/elements/favicon.ico">
    <link rel="stylesheet" type="text/css" href="assets/css/reset.css">
    <link rel="stylesheet" type="text/css" href="assets/css/core.css">
    <link rel="stylesheet" type="text/css" media="screen and (max-width : 1030px)" href="assets/css/tablets-landscape.css"/>
    <link rel="stylesheet" type="text/css" media="screen and (max-width : 770px)" href="assets/css/tablets-portrait.css"/>
    <link rel="stylesheet" type="text/css" media="screen and (max-width : 500px)" href="assets/css/phone.css"/>             
    <!--Libraries-->
    <script type="text/javascript" src="assets/libraries/jquery.js"></script>
</head>

<body>
    <header><!--Header-->
        <div id="headerWrapper">
            <div id="headerContent">

                <div id="headerLogo">
                    <a href="index.php"><img alt="Logo" src="assets/elements/logo.png"/></a>
                </div>

                <nav><ul id="mainMenu"><!--Main Menu-->
                    <li><a class="active" href="#section1">Home</a></li>
                    <li><a href="#section2">About us</a></li>
                    <li><a href="#section3">Endorsements</a></li>
                    <li><a href="#section4">Portfolio</a></li>
                    <li><a href="#section5">Contact us</a></li>
                </ul></nav>

            </div>
        </div>
    </header>

    <div id="Page"><!--Website Content-->       

        <div id="secondHeader">
            <a class="secondHeader-menuButton" href="#">Menu</a>
            <p id="logo-smallScreen">Spacehopper Design</p>
        </div>

        <div id="section1" class="wrapperA"><!--Page Content-->
            <div id="home-sectionA" class="content" >
                <img src="assets/elements/home-sectionA-background.png">
            </div>
        </div>  

        <div id="section2" class="wrapperB"><!--Page Content-->
            <div id="home-sectionB" class="content" >
                <img src="assets/elements/home-sectionB-background.png">
                <div id="home-sectionB-textbox1">
                    <h3>...three words that epitomise Spacehopper Design​​​​.</h3><br>
                    <p>We strongly believe that everything begins with an idea, and that there is a big idea hidden in all of us.</p>
                    <p>You may have that Eureka moment, but are unsure how to develop this idea further from an upbeat, design perspective.</p>
                    <p>This is where Spacehopper Design can help. We are a fast, focused and forward-thinking conceptual agency who promise to find an intuitive solution to your design needs. And of course, we’ll have fun doing so.</p>
                    <p>With our wealth of experience in editorial design, print design and branding, we also have a deep knowledge of understanding our target audience requirements, from conceptual stage right through to the final product.</p>​
                    <p>So make that leap and let’s work together on making your idea a visual treat and a reality!​​​​​&lt;/p>
                </div>
            </div>
        </div>  

        <div id="section3" class="wrapperA"><!--Page Content-->
            <div id="home-sectionC" class="content" >
                <img src="assets/elements/home-sectionC-background.png">    

                <div id="portfolioGallery">

                    <div class="item">
                        <img alt="" src="assets/images/audiobullys.jpg">
                        <div>
                            <h3>Title</h3>
                            <p>Disruption Disruption Disruption</p>
                        </div>
                    </div>

                    <div class="item">
                        <img alt="" src="assets/images/audiobullys.jpg">
                        <div>
                            <h3>Title</h3>
                            <p>Disruption Disruption Disruption</p>
                        </div>
                    </div>

                    <div class="item">
                        <img alt="" src="assets/images/audiobullys.jpg">
                        <div>
                            <h3>Title</h3>
                            <p>Disruption Disruption Disruption</p>
                        </div>
                    </div>

                    <div class="item">
                        <img alt="" src="assets/images/audiobullys.jpg">
                        <div>
                            <h3>Title</h3>
                            <p>Disruption Disruption Disruption</p>
                        </div>
                    </div>

                    <div class="item">
                        <img alt="" src="assets/images/nta2.jpg">
                        <div>
                            <h3>Title</h3>
                            <p>Disruption Disruption Disruption</p>
                        </div>
                    </div>

                    <div class="item">
                        <img alt="" src="assets/images/look.jpg">
                        <div>
                            <h3>Title</h3>
                            <p>Disruption Disruption Disruption</p>
                        </div>
                    </div>

                    <div class="item">
                        <img alt="" src="assets/images/open-bookheat.jpg">
                        <div>
                            <h3>Title</h3>
                            <p>Disruption Disruption Disruption</p>
                        </div>
                    </div>

                </div>
            </div>  
        </div>  

        <div id="section4" id="1" class="wrapperB"><!--Page Content-->
            <div id="home-sectionD" class="content" >

                <div class="home-sectionD-textbox">
                    <h3>Reesa Amadeo Wolf: Entrepreneur MD at Fresh Young Media </h3>
                    <p>We knew Srt of the bigger vision for what we do assion for what we do assion for what we do assion for what weas a social enterprise. We will be working with Spacehopper Design for a long time!</p>
                </div>

                <div class="home-sectionD-textbox">
                    <h3>Reesa Amadeo Wolf: Entrepreneur MD at Fresh Young Media </h3>
                    <p>We knew Srt of the bigger vision for what we do assion for what we do assion for what we do assion for what weas a social enterprise. We will be working with Spacehopper Design for a long time!</p>
                </div>

                <div class="home-sectionD-textbox">
                    <h3>Reesa Amadeo Wolf: Entrepreneur MD at Fresh Young Media </h3>
                    <p>We knew Srt of the bigger vision for what we do assion for what we do assion for what we do assion for what weas a social enterprise. We will be working with Spacehopper Design for a long time!</p>
                </div>

                <div class="home-sectionD-textbox">
                    <h3>Reesa Amadeo Wolf: Entrepreneur MD at Fresh Young Media </h3>
                    <p>We knew Srt of the bigger vision for what we do assion for what we do assion for what we do assion for what weas a social enterprise. We will be working with Spacehopper Design for a long time!</p>
                </div>

                <div class="home-sectionD-textbox">
                    <h3>Reesa Amadeo Wolf: Entrepreneur MD at Fresh Young Media </h3>
                    <p>We knew Srt of the bigger vision for what we do assion for what we do assion for what we do assion for what weas a social enterprise. We will be working with Spacehopper Design for a long time!</p>
                </div>

                <div class="home-sectionD-textbox">
                    <h3>Reesa Amadeo Wolf: Entrepreneur MD at Fresh Young Media </h3>
                    <p>We knew Srt of the bigger vision for what we do assion for what we do assion for what we do assion for what weas a social enterprise. We will be working with Spacehopper Design for a long time!</p>
                </div>

                <div class="home-sectionD-textbox">
                    <h3>Reesa Amadeo Wolf: Entrepreneur MD at Fresh Young Media </h3>
                    <p>We knew Srt of the bigger vision for what we do assion for what we do assion for what we do assion for what weas a social enterprise. We will be working with Spacehopper Design for a long time!</p>
                </div>

                <img src="assets/elements/home-sectionD-background.png">
            </div>
        </div>      

        <div id="section5" class="contactSectionWrapper"><!--Page Content-->
            <div id="contactSection" class="content" >

                <h2>Want to Get in Touch?</h2>              

                <div id="grid1">
                    <form id="contactForm" action="email.php" method="post">
                        <label for="name">Your Name</label>
                        <input class="input" id="name" type="text" required placeholder="Enter Name">

                        <label for="email">Email Address</label>
                        <input class="input" id="email" type="email" required placeholder="Enter Email">

                        <label for="message">Message</label>
                        <textarea class="input" id="message" required></textarea>

                        <button id="submit" type="submit">Send</button>
                    </form>

                    <div id="contact-info">
                        <p><strong>Email:</strong> Support@LoaiDesign.co.uk</p>
                        <p><strong>Telephone:</strong> +44 0 745326 2993</p>
                        <p><strong>Skype:</strong> Loai.Bassam</p>
                    </div>
                        <br>
                    <div class="socialbar icons">
                        <a href="https://www.facebook.com/LOAI.Design.Studio" class="facebook" target="_blank"><span>Facebook</span></a>
                        <a href="https://twitter.com/LoaiBassam" class="twitter" target="_blank"><span>Twitter</span></a>
                        <a href="http://uk.linkedin.com/in/loaibassam" class="linkedin" target="_blank"><span>Linkedin</span></a>
                    </div>
                </div>  

                <div id="grid2">
                    <img alt="" src="assets/elements/map.png" id="map">
                </div>      

            </div>  
        </div>

        <footer id="footer"><!--Footer Section-->
            <div id="footerContent">
                <p>Copyright ©2013 Spacehopper Design. All rights reserved.</p><a href="http://www.loaidesign.co.uk" style="padding-top: 20px;">Website Designed By<img style="display: inline-block; margin-left: 5px; vertical-align: middle;" alt="loai design studio responsive website design" src="assets/elements/loaidesignstudio-logo.png"></a>
            </div>
        </footer>

    </div><!--The End Of The Page-->    
    <!--Scripts Links-->
    <script type="text/javascript" src="assets/js/slider.js"></script>
    <script type="text/javascript" src="assets/js/main.js"></script>
    <script src="assets/libraries/modernizr.js"></script>
</body>

body {
    background-color: #FFFFFF;

    line-height: 125%;
    text-align: center;  
    overflow-x: hidden;

    -webkit-text-size-adjust: none;
    -webkit-overflow-scrolling: touch;  
}

/*Font Face*/
@font-face { font-family: Comfortaa; src: url('Comfortaa-Light.ttf'); } 
@font-face { font-family: Comfortaa; font-weight: bold; src: url('Comfortaa-Bold.ttf');}
/*Fonts*/h1, h2, h3, h4, h5, h6, p, pre, a, ol, li, span, label, blockquote, figcaption, abbr, td, input, textarea {
    font-family: Comfortaa, sans-serif; 
    font-size: 15px;
    color: #636363;
}

h1 {
    font-size: 26px;
    line-height: 120%;
    font-weight: bold;
}

h2 {
    font-size: 26px;
    line-height: 120%;
    font-weight: bold;
}

h3 {
    font-size: 20px;
    font-weight: bold;
}

small {
    font-size: 13px;
}

strong {
    font-weight: bold;
}

em {
    font-style: italic; 
}

img {
    display: block;
}


/*MAIN/////////////////////////////////////////////////////////////////////*/   
/*Main Page*/#Page {    
    width: 100%;
    position: relative; 
    margin-top: 103px;
    overflow: hidden;
}

/*Wrappers*/.wrapper, .wrapperA, .wrapperB, .contactSectionWrapper { width: 100%; overflow: hidden; }

    .wrapper {
        background-color: #FFFFFF;
    }

    .wrapperA {
        background-color: #F58221;
    }

    .wrapperB {
        background-color: #23408E;
    }

/*Content Container*/.content {
    width: 1030px;
    padding: 0 20px;
    margin: auto;   
    overflow: hidden;
}


/*HEADER///////////////////////////////////////////////////////////////////*/
/*Header Wrapper*/#headerWrapper {
    background-color: #ffffff;
    width: 100%;
    position: fixed; 
    top: 0; left: 0; 
    z-index: 1000;

    -webkit-box-shadow: 0px 0px 25px -1px rgba(0,0,0,0.3);
    -moz-box-shadow: 0px 0px 25px -1px rgba(0,0,0,0.3);
    box-shadow: 0px 0px 25px -1px rgba(0,0,0,0.3);
}

/*Header Content Container*/#headerContent {
    width: 1030px;
    padding: 10px 20px;
    margin: auto;   
    overflow: auto;
}

/*Header Logo*/#headerLogo {
    width: 300px;
    float: left;
}

/*Main Menu*/#mainMenu {
    margin-top: 20px;
    float: right;
}

    #mainMenu li {
        float: left;
    }

    #mainMenu a {
        color: #F58221;

        padding: 10px 15px;
        margin-left: 5px;

        border-radius: 3px;
        -moz-border-radius: 3px;
        -webkit-border-radius: 3px;

        -webkit-transition: background 0.1s linear;
        -moz-transition: background 0.1s linear;
        -ms-transition: background 0.1s linear;
        -o-transition: background 0.1s linear;
        transition: background 0.1s linear;
    }

    #mainMenu a:hover {
        color: #23408E; 
    }   

    #mainMenu a.active {
        color: #7E7E7E;
        background-color: #FAFAFA;
        cursor: default;
    }

/*Tablet & Phone Header (Hidden From Widescreen's)*/
.secondHeader-menuButton, #secondHeader, #logo-smallScreen {
    display: none;
}



/*FOOTER////////////////////////////////////////////////////////////////////*/  
/*Footer Section*/
#footer {
    border-top: 1px solid #192E66;
    background-color: #23408E;
    width: 100%;
}

#footerContent {
    width: 1030px;
    padding: 20px;
    margin: auto;   
    overflow: hidden;
}

    #footerContent * {
        color: #FFFFFF;
        font-size: 13px;
    }



/*PAGES////////////////////////////////////////////////////////////////////*/   
/*About Page*/  
#home-sectionB {
    position: relative;
}

    #home-sectionB-textbox1 {
        width: 500px;
        max-width: 100%;

        position: absolute;
        bottom: 135px;
        right: 50px;

        text-align: justify;
    }

        #home-sectionB-textbox1 p, h3 {
            color: #FFFFFF;
        }



/*Portfolio Page*/  
#home-sectionC {
    padding-bottom: 50px;
}

    #portfolioGallery * {
        color: #FFFFFF;
    }

#portfolioGallery .item {
    border: 1px dashed #FFFFFF;
    width: 310px; height: 250px;
    float: left;
    margin: 5px;
    overflow: hidden;
    cursor: pointer;

    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
}

    #portfolioGallery .item div {
        background-color: #23408E;  
        width: 100%; height: 100%;
        padding: 100px 20px 20px 20px;
    }

    #portfolioGallery .item img {
        max-width: 380px; 
    }

        #portfolioGallery .item:hover > img {
            display: none;
        }



/*Endorsements*/    
#home-sectionD {
    padding-top: 50px;
}

.home-sectionD-textbox * {
    color: #FFFFFF;
}

.home-sectionD-textbox {
    padding: 30px;
    text-align: left;
    clear: both;
}

    .home-sectionD-textbox:last-of-type {
        padding: 30px 30px 0 30px;
    }

    .home-sectionD-textbox:before {
        content: url('../elements/quote.png');
        padding-right: 20px;
        float: left;
    }



/*Contact Page*/
.contactSectionWrapper {
    background-color: #FFFFFF;
    background-image: url('../elements/strip.png');
    background-repeat: repeat-x;
    background-position: top;
    padding: 50px 0;
}

#contactSection h2 {
    padding-top: 50px;
    color: #F58221;
    font-size: 50px;
}

#grid1 {
    width: 40%;
    vertical-align: middle;
    display: inline-block;
    margin-left: -2px;
    padding: 50px 0;
}

#grid2 {
    width: 60%;
    vertical-align: middle;
    display: inline-block;
    margin-left: -2px;
    padding: 0 0 0 50px;    
}

    #contactForm label{
        color: #23408E;
        display: block;
        padding: 10px 0;
        text-align: left;
    }

    #contactForm input, 
    #contactForm textarea {     
        width: 100%;
        display: block;
        padding: 10px 10px;
        margin-bottom: 15px;

        text-align: left;
        outline: none;

        border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    }

    #contactForm textarea{
        resize: vertical;
    }

    #submit{
        color: #FFFFFF;
        background: #23408E;
        border: none;

        width: 100%;
        padding: 10px 10px;

        cursor: pointer;
        font-size: 15px;

        border-radius: 5px;
        -moz-border-radius: 5px;
        -webkit-border-radius: 5px;
    }   

    #submit:hover, 
    #submit:active {
        background: #F58221;
    }   

.notSelected {
    color: #B2B2B2;
    border: 1px solid #23408E;
}

.selected {
    color: #F58221;
    border: 1px solid #F58221;
}

#contact-info {
    padding-top: 40px;
}

/*Social Media Icons*/
.socialbar {
    padding-top: 5px;
    display: inline-block;
    cursor: default;
}

    .icons a {
        background-image:url("../elements/socialmediaicons.png");
        background-repeat: no-repeat;
    }

    .icons a {
        display: inline-block;
        height: 40px;
        width: 40px;
    }

        .icons a span {
            display: none;
        }

    .icons a.facebook { background-position: 0 -160px; }
    .icons a.facebook:hover { background-position: 0 -200px; }

    .icons a.linkedin { background-position: 0 -720px; }
    .icons a.linkedin:hover { background-position: 0 -760px; }

    .icons a.twitter { background-position: 0 -1520px; }
    .icons a.twitter:hover { background-position: 0 -1560px; }  
4

1 回答 1

0

实际上,有一个小脚本可以为您执行此操作。

$(document).ready(function() {
  function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.offset().top;
        $(this).click(function(event) {
          event.preventDefault();
          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
            location.hash = target;
          });
        });
      }
    }
  });

  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }

});

仅供参考,此脚本取自CSS Tricks

于 2013-09-24T11:27:55.537 回答