[英]Display: table-cell absolute positioning query
我正在为wordpress构建一个下拉菜单,但遇到了一个有趣的难题。 最初,当我将鼠标悬停在显示所有类别的“商店”时,我会出现一个下拉菜单,从其突出显示的类别中,可以在div下方的div中调出5个随机产品,这些产品将覆盖菜单栏的整个宽度。 这很好
但是,当时的客户认为他需要的类别比那里的要多得多 ,而我不得不将它们细分为子类别。
菜单当前的工作方式如下:
悬停商店->显示主要类别悬停类别->显示子类别。
这部分工作正常; 但是,使用css表设置对显示子类别的列表进行布局,以使子类别列表在菜单大小上均匀分布。 您可以在此处看到我的意思的示例。
以前,我没有使用css表设置,因为菜单非常合适。 包含类别产品的下拉div效果很好。 现在,尽管它们没有在CSS表标记中定义,但它们似乎已经“假装”为表单元格。 产品的每个下拉列表都在每个子类别的右侧放置空白。 但是,由于它们将均匀分布,因此看起来很奇怪。
所以我有两个问题:第一。 尽管绝对显示,但下拉产品部分却不在我需要的地方。 有没有办法打破这些表布局?
其次,如果没有,无论我有多少个子类别,我还需要哪些其他选择才能使这些类别在父div上均匀显示? 我考虑的一个选项是使用javascript计算出所需的宽度百分比,然后将其直接应用于li,但是我不确定那是多么可行。
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
$categorycounter == 0;
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$categorycounter = $categorycounter + 1;
$categorypadding = "";
$category_id = $cat->term_id;
echo $categorypadding . '<li class="category-' . $categorycounter. '"><a class="main-nav-link" href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a><div class="drop-down-'. $cat->name .'"><section class="drop-down-section"><section class="sub-list">';
// set up subcategories to display below main categories
$IDbyNAME = get_term_by('name', $cat->name, 'product_cat');
$product_cat_ID = $IDbyNAME->term_id;
$args4 = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $product_cat_ID,
'taxonomy' => 'product_cat',
'posts_per_page' => -1
);
$subcategoryloop = get_categories( $args4 );
$subcount = 0;
foreach ($subcategoryloop as $sc){
if($subcategoryloop){
$subcount = $subcount + 1;
$link = get_term_link( $sc->slug, $sc->taxonomy);
echo '<a class="subme" href="' . $link . '">'. $sc->name .'</a><section class="drop-down-products">';
//set up subcategories to display products
$args2 = array(
'posts_per_page' => -1,
'product_cat' => $sc->slug,
'post_type'=>'product',
'orderby' => 'rand'
);
$productcount = 0;
$loop = new WP_Query( $args2 );
while ( $loop->have_posts() ) : $loop->the_post();
$productcount = $productcount + 1;
if($productcount < 6){
echo "<section class='thumbcontainer'><p><a href='";
the_permalink();
echo "'>" ;
the_title();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo "</a></p><img src='" . $feat_image . "'/><br/><p class='dropdown-price'>";
$price = get_post_meta( get_the_ID(), '_regular_price', true);
echo "£" .$price;
echo "</p></section>";
}
endwhile;
echo "<section><a class='view-more' href='". get_term_link($cat->slug, 'product_cat') ."'>View More Products</a></section></section>";
}
}
echo "</section></section></div></li>";
}
}
附带一提,我知道我的代码很乱。 抱歉!
要证明列表的合理性,请添加:
.sub-list {
display: table;
table-layout: fixed;
}
.subme {
width: 100%;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.