我正在尝试为我的WordPress站点(www.carryoneverything.com)创建粘性标题。我已经安装了最新的店面主题,但我似乎不能得到我的标志,导航,购物车和搜索所有在同一行。我想是因为它们都在单独的div中吧?
我希望我的头看起来像https://www.hollisterco.com/shop/us当它完成。
此外,当我这样做时,管理栏会遮盖粘性标题,粘性标题会遮盖正文:
#masthead {
position: fixed;
top: 0;
width: 100%;
}我能用CSS解决这个问题吗?还是必须使用javascript?
发布于 2017-12-12 08:22:25
你绝对可以用CSS解决这个问题。容纳你想要排列的项目的父容器可以添加flex属性,这样它就会为你排列东西。有关flex的更多信息,请单击此处:https://css-tricks.com/snippets/css/a-guide-to-flexbox/
当通过给div一个固定的位置来掩盖事情时,很可能是z索引的问题。这使您可以分层div,将某些div放在顶部,而将其他div放在底部。
示例:z-index:2将覆盖z-index:1。有关z-index的更多信息,请访问:https://www.w3schools.com/cssref/pr_pos_z-index.asp
.parent{
display:flex;
justify-content:space-between;
align-items:center;
}
.child{
width:20%;
background-color:black;
color:white;
text-align:center;
}<div class="parent">
<div class="child">
1
</div><!-- child -->
<div class="child">
2
</div><!-- child -->
<div class="child">
3
</div><!-- child -->
</div><!-- parent -->
发布于 2017-12-12 08:44:31
这是一个wordpress问题。我们不能只给你一个代码就能解决它。我们必须有访问网站的权限。主题还可以为您提供一些编辑(主题选项)。实际上是个小问题,顺便说一句。
发布于 2019-08-21 02:50:57
将css添加到style.css
#masthead {
position: fixed;
top: 0;
width: 100%;
}
@media screen and (min-width: 768px) {
/*resizing the logo image */
#masthead.sticky .custom-logo-link img {
width: auto;
height: 40px;
}
/*positioning the main-navigation */
#masthead.sticky .main-navigation {
text-align: right;
position: fixed;
top: 0;
right: 300px;
padding: 0;
width: auto;
}
/*positioning the logo*/
#masthead.sticky .custom-logo-link {
position: fixed;
top: 0;
margin: 0;
padding: 0;
}
/*adjusting default margins and paddings*/
#masthead.sticky .site-header-cart .cart-contents{
padding:1em 0;
}
#masthead.sticky .main-navigation ul.menu>li>a, .main-navigation ul.nav-menu>li>a {
padding: 1em 1em;
}
#masthead.sticky .site-branding{
margin-bottom: 1em;
}
/*positioning the cart-menu */
#masthead.sticky .site-header-cart {
width: 14% !important;
position: fixed !important;
top: 0;
right: 12%;
padding: 0;
}
/*applying the position fixed on the masterhead */
#masthead.sticky{
position: fixed;
top: 0;
width: 100%;
}
/*removing the site search*/
#masthead.sticky .site-search{
display:none;
}
}在主题的js文件夹中创建一个“stickyheader.js”文件。并添加以下代码
(function($){
$(document).ready(function () {
$(window).scroll(function() {
if ($(this).scrollTop() > 100){
jQuery('#masthead').addClass('sticky');
}
else{
jQuery('#masthead').removeClass("sticky");
}
});
});
})(jQuery);将此代码添加到functions.php中,以便包含js文件
$path = get_stylesheet_directory_uri() .'/js/';
if (!is_admin()) wp_enqueue_script('stickyheader', $path.'stickyheader.js',
array('jquery'));https://stackoverflow.com/questions/47763389
复制相似问题