我想要一个元素,在这种情况下称为neo_about_nav,在滚动到称为#neo_about_section的div时,保持固定在其位置。
这是我的SASS
/* Container for "About" section */
#neo_about_section {
width: 960px;
height: 250px;
background: #FFF;
margin: 0 auto;
/* Container for centered text */
.neo_text_container {
width: 480px;
text-align: center;
color: #000;
float: left;
margin: 25px 90px 0 240px;
/* About section "Read More" button */
#neo_about_button {
width: 150px;
height: 25px;
background-color: #F90;
color: #FFF;
text-align: center;
line-height: 25px;
margin: 0 auto;
margin-top: 15px;
cursor: pointer;
display: table; /* Fix to make text center aligned */
} /* About button end */
} /* Text container end */
/* About section navigation that scrolls to sections on home page */
.neo_about_nav {
position: absolute;
width: 150px;
height: 200px;
background: #CCC;
color: #000;
float: left;
margin: 25px 0 0 820px;
z-index: 500;
/* Styling for navigation list */
ul {list-style: none; padding-left: none; font-size: 14px;}
li {
background: #FFF;
margin: 10px 0 0 -40px;
cursor: pointer;
a {
/* Formatting for list items */
&:link {text-decoration: none; color: #FFFFFF;}
&:hover {color: #CCC;}
&:active {color: #222;}
&:visited {text-decoration:none; color: #FFF;}
}
}
} /* About section nav end */
} /* About section end */
这是HTML
<main role="main">
<!-- section -->
<section>
<?php //the_title(); ?></h1> <!-- Comment out if page title not needed -->
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="neo_container"> <!-- Container start -->
<div id="neo_homeimage"> <!-- Brand statement image -->
<h1 class="neo_homeimage_text"><?php the_field('home_image_text'); ?></h1>
</div> <!-- Brand statement image end -->
<div id="neo_about_section"> <!-- About section start -->
<div class="neo_text_container"> <!-- About text container -->
<h1>About the Grossman Companies, Inc.</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div id="neo_about_button"> <!-- Read more button -->
<p>Read More</p>
</div> <!-- Read more button end -->
</div> <!-- Text container end -->
<div class="neo_about_nav">
<ul>
<li>About</li>
<li>Services Overview</li>
<li>Featured Properties</li>
<li>Current News</li>
</ul>
</div>
</div> <!-- About section end -->
<div id="neo_accord_section"> <!-- Accordion section start -->
<div>
<input id="ac-1" name="accordion-1" type="radio" />
<label for="ac-1">Acquisitions</label>
<article class="ac-small">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt</p>
</article>
</div>
<div>
<input id="ac-2" name="accordion-1" type="radio" />
<label for="ac-2">Private Lending</label>
<article class="ac-small">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt</p>
</article>
</div>
<div>
<input id="ac-3" name="accordion-1" type="radio"/>
<label for="ac-3">Brokerage</label>
<article class="ac-small">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt</p>
</article>
</div>
<div>
<input id="ac-4" name="accordion-1" type="radio"/>
<label for="ac-4">Property Management</label>
<article class="ac-small">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt</p>
</article>
</div>
</div> <!-- Accordion section end -->
<div id="neo_featprop_section"> <!-- Featured poperties section -->
<div id="neo_featprop_header"> <!-- Featured properties header -->
<h1 class="neo_featprop_headertext">Featured Properties</h1>
</div> <!-- Featured properties header end -->
<div id="neo_featprop_gallery"> <!-- Featured properties gallery start -->
<!-- First Row -->
<div class="neo_gall_img">
<!--<div class="neo_img_overlay"></div>--> <!-- Darkened img overlay -->
<div class="neo_gall_textcontainer"> <!-- Text container for indiv. gall img -->
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<div class="neo_gall_img_button"> <!-- View more button -->
<p>View</p>
</div> <!-- View more button -->
</div> <!-- Gall img text container end -->
</div> <!-- Img end -->
<div class="neo_gall_img">
<!--<div class="neo_img_overlay"></div>--> <!-- Darkened img overlay -->
<div class="neo_gall_textcontainer"> <!-- Text container for indiv. gall img -->
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<div class="neo_gall_img_button"> <!-- View more button -->
<p>View</p>
</div> <!-- View more button -->
</div> <!-- Gall img text container end -->
</div> <!-- Img end -->
<div class="neo_gall_img">
<!--<div class="neo_img_overlay"></div>--> <!-- Darkened img overlay -->
<div class="neo_gall_textcontainer"> <!-- Text container for indiv. gall img -->
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<div class="neo_gall_img_button"> <!-- View more button -->
<p>View</p>
</div> <!-- View more button-->
</div> <!-- Gall img text container end -->
</div> <!-- Img end -->
<!-- Second Row -->
<div class="neo_gall_img">
<!--<div class="neo_img_overlay"></div>--> <!-- Darkened img overlay -->
<div class="neo_gall_textcontainer"> <!-- Text container for indiv. gall img -->
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<div class="neo_gall_img_button"> <!-- View more button -->
<p>View</p>
</div> <!-- View more button-->
</div> <!-- Gall img text container end -->
</div> <!-- Img end -->
<div class="neo_gall_img">
<!--<div class="neo_img_overlay"></div>--> <!-- Darkened img overlay -->
<div class="neo_gall_textcontainer"> <!-- Text container for indiv. gall img -->
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<div class="neo_gall_img_button"> <!-- View more button -->
<p>View</p>
</div> <!-- View more button-->
</div> <!-- Gall img text container end -->
</div> <!-- Img end -->
<div class="neo_gall_img">
<!--<div class="neo_img_overlay"></div>--> <!-- Darkened img overlay -->
<div class="neo_gall_textcontainer"> <!-- Text container for indiv. gall img -->
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<div class="neo_gall_img_button"> <!-- View more button -->
<p>View</p>
</div> <!-- View more button-->
</div> <!-- Gall img text container end -->
</div> <!-- Img end -->
</div> <!-- Featured properties gallery end -->
</div> <!-- Featured poperties section end -->
<div id="neo_currnews_section"> <!-- Current news section -->
<div id="neo_currnews_header"> <!-- Current new header -->
<h1 class="neo_currnews_headertext">Current News</h1>
</div> <!-- Current news header end-->
<div id="neo_currnews_content"> <!-- Current news main content -->
<div id="neo_currnews_gall"> <!-- Current new gallery -->
<div class="neo_currnews_gall_img1">
<img src="http://fpoimagery.com/?t=px&w=310&h=225&bg=0ff&fg=000000" />
<h2 class="gall_img_text">Property Name</h2>
<h2 class="gall_img_text">Leases</h2>
<p class="gall_img_text">Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
</div>
<div class="neo_currnews_gall_img2">
<img src="http://fpoimagery.com/?t=px&w=310&h=225&bg=ff00&fg=000000" />
<h2 class="gall_img_text">Property Name</h2>
<h2 class="gall_img_text">Leases</h2>
<p class="gall_img_text">Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
</div>
<div class="neo_currnews_gall_img3">
<img src="http://fpoimagery.com/?t=px&w=310&h=225&bg=0000cc&fg=000000" />
<h2 class="gall_img_text">Property Name</h2>
<h2 class="gall_img_text">Leases</h2>
<p class="gall_img_text">Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
</div>
</div> <!-- Curent news gallery end -->
<div id="neo_currnews_panel"> <!-- Current news expandable info panel -->
<div id="neo_currnews_panel_header"> <!-- Info panel header -->
<h2>News</h2>
<h2>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</h2>
</div> <!-- Info panel header end -->
<div id="neo_currnews_panel_content"> <!-- Info panel content -->
<div class="neo_currnews_panel_column"> <!-- Left column -->
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p>
</div> <!-- Left column end -->
<div class="neo_currnews_panel_column"> <!-- Middle column -->
<p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.</p>
<p>Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae.</p>
</div> <!-- Middle column end -->
<div class="neo_currnews_panel_column2">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p>
</div>
</div> <!-- Info panel content end -->
</div> <!-- Current news info panel end -->
</div> <!-- Current news content end -->
</div> <!-- End current news section -->
</div> <!-- Container end -->
<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_content(); ?>
<?php comments_template( '', true ); // Remove if you don't want comments ?>
<br class="clear">
<?php edit_post_link(); ?>
</article>
<!-- /article -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'grossman' ); ?></h2>
</article>
<!-- /article -->
<?php endif; ?>
</section>
<!-- /section -->
</main>
我尝试了几种解决方案,但似乎没有一种适合我的情况。完整的网站可以在这里http://neoscapelabs.com/projects/grossman_corp/
这是使用HTML5空白框架的WordPress网站
最佳答案
步骤1
放在您的jquery文件或标头部分
$(window).scroll(function(){
var p = $( ".your-div" ).height();
if ($(this).scrollTop() > p) {
$('.fixeddiv').addClass('fixed');
} else {
$('.fixeddiv').removeClass('fixed');
}
});
第2步
放在您的CSS文件中
.fixed{ position:fixed; top:0; z-index:1000; width:100%; background:#FFF; }