Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

php - CSS- ckeck and add right arrow where li has ul

I want to add css right arrow where ul has a ul child? Below is triangle right css. I want to add in my menu so that users know this menu has sub menu.

How is it possible to detect in pure css if a li has children of ul.children?

I want to add below triangle-right arrow to my css. Plz help.

php code for the menu:

<?php
$stmt = $pdo->query('SELECT * FROM `category` where `parent_id` = 0');
$stmt->execute();
?>

<ul class="top-level-menu">
    <?php while($menu1 = $stmt->fetch()){ ?>
        <li><a href="<?php echo $menu1['category_link'] . "
"; ?>"><?php echo $menu1['product'] . "
"; ?></a>

            <?php   $stmt1 = $pdo->prepare('SELECT * FROM category WHERE parent_id = ?');
                    $stmt1->execute([$menu1['id']]);
                    ?>
                    <ul class="second-level-menu">
                        <?php while($menu2 = $stmt1->fetch()){ ?>
                        <li><a href="<?php echo $menu2['category_link'] . "
"; ?>"><?php echo $menu2['product'] . "
"; ?></a>
                                    <?php
                                    $stmt2 = $pdo->prepare('SELECT * FROM category WHERE parent_id = ?');
                                    $stmt2->execute([$menu2['id']]);
                                    ?>
                                <ul class="third-level-menu">
                                    <?php while($menu3 = $stmt2->fetch()){ ?>
                                   <li><a href="<?php echo $menu3['category_link'] . "
"; ?>"><?php echo $menu3['product'] . "
"; ?></a>
                                   </li>
                                   <?php } ?>
                                </ul>

                        </li>

                        <?php } ?>
                    </ul>


        </li>
<?php } ?>
</ul>

css for the menu bar

<style>
/* Menu Styles */

.third-level-menu
{
    position: absolute;
    top: 0;
    right: -220px;
    width: 220px;
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}

.third-level-menu > li
{
    height: 30px;
    background: #999999;
}
.third-level-menu > li:hover { background: #CCCCCC; }

.second-level-menu
{
    position: absolute;
    top: 30px;
    left: 0;
    width: 200px;
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}

.second-level-menu > li
{
    position: relative;
    height: 30px;
    background: #999999;
}
.second-level-menu > li:hover { background: #CCCCCC; }

.top-level-menu
{
    list-style: none;
    padding: 0;
    margin: 0;
}

.top-level-menu > li
{
    position: relative;
    float: left;
    height: 30px;
    width: 100px;
    background: #999999;
}
.top-level-menu > li:hover { background: #CCCCCC; }

.top-level-menu li:hover > ul
{
    /* On hover, display the next level's menu */
    display: inline;
}


/* Menu Link Styles */

.top-level-menu a /* Apply to all links inside the multi-level menu */
{
    font: bold 14px Arial, Helvetica, sans-serif;
    color: #FFFFFF;
    text-decoration: none;
    padding: 0 0 0 10px;

    /* Make the link cover the entire list item-container */
    display: block;
    line-height: 30px;
}
.top-level-menu a:hover { color: #000000; }
</style>

check my menu where there is no arrow where li has ul

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So there's really no parent selector in CSS, but as you've tagged this with jQuery, we can use the :has() selector to do much the same behavior. Basically, if a LI el contains a UL, we should assume the UL is a submenu, and I will add the 'js-has-a-submenu' class to the anchor el that we're using for the menu items. I've prefixed it with 'js-' simply to reflect that it isn't a statically applied class, but one we'll only use via js.

The code here is actually taken from the menu example I'd referenced in the commments, but then I noticed they'd hard-coded the down-arrow into the menu, which really breaks what you're trying to do, from what I understand. So I removed the down arrow, and used jQuery to apply the class. To understand the menus themselves, please do go read this, as I think it does pretty well (not a fan of the pink, but its css).

$("li:has(ul)").find("a:eq(0)").addClass("js-has-a-submenu");
body {
  background: #333;
}

.clearfix:after {
  display: block;
  clear: both;
}


/*----- Menu Outline -----*/

.menu-wrap {
  width: 100%;
  box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.2);
  background: #3e3436;
}

.menu {
  width: 1000px;
  margin: 0px auto;
}

.menu li {
  margin: 0px;
  list-style: none;
  font-family: 'Ek Mukta';
}

.menu a {
  text-decoration:none;
  transition: all linear 0.15s;
  color: #919191;
}

.menu li:hover > a,
.menu .current-item > a {
  text-decoration: none;
  color: #be5b70;
}

.menu .arrow {
  font-size: 11px;
  line-height: 0%;
}


/*----- Top Level -----*/

.menu > ul > li {
  float: left;
  display: inline-block;
  position: relative;
  font-size: 19px;
}

.menu > ul > li > a {
  padding: 10px 40px;
  display: inline-block;
  text-shadow: 0px 1px 0px rgba(0, 0, 0, 0.4);
}

.menu > ul > li:hover > a,
.menu > ul > .current-item > a {
  background: #2e2728;
}


/*----- Bottom Level -----*/

.menu li:hover .sub-menu {
  z-index: 1;
  opacity: 1;
}

.sub-menu {
  width: 160%;
  padding: 5px 0px;
  position: absolute;
  top: 100%;
  left: 0px;
  z-index: -1;
  opacity: 0;
  transition: opacity linear 0.15s;
  box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.2);
  background: #2e2728;
}

.sub-menu li {
  display: block;
  font-size: 16px;
}

.sub-menu li a {
  padding: 10px 30px;
  display: block;
}

.sub-menu li a:hover,
.sub-menu .current-item a {
  background: #3e3436;
}

.js-has-a-submenu::after {
     content: " 2193";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="menu-wrap">
  <nav class="menu">
    <ul class="clearfix">
      <li><a href="#">Home</a></li>
      <li>
        <a href="#">Movies </a>

        <ul class="sub-menu">
          <li><a href="#">In Cinemas Now</a></li>
          <li><a href="#">Coming Soon</a></li>
          <li><a href="#">On DVD/Blu-ray</a></li>
          <li><a href="#">Showtimes &amp; Tickets</a></li>
        </ul>
      </li>
      <li><a href="#">T.V. Shows</a></li>
      <li><a href="#">Photos</a></li>
      <li><a href="#">Site Help</a></li>
    </ul>
  </nav>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...