如何在主菜单悬停时打开子菜单?

时间:2013-12-23 11:17:01

标签: html css menu submenu

您好我正在制作菜单和子菜单。我做了所有效果的菜单。我想在菜单项悬停时打开子菜单,但它没有打开;它表明我已经开放了。

我的JSFiddle

#companymenu {
    background-color: #999;
    height:35px;
    width:100%;
    margin-top: -10px;
}
.companymenuul {
    list-style-type: none;
}
.companymenuli {
    float: left;
    display:block;
    line-height: 35px;
    padding: 0 15px;
}
.alisting {
    color:#000;
    text-decoration:none;
}
.aactive {
    color: #333;
    background-color: #fff;
    border-top: 2px solid #999;
    margin-top: -2px;
}
.companymenuli a:hover {
    color:#C63;
    text-decoration:none;
    cursor:pointer;
    padding-top:10px;
    padding-left:24px;
    padding-right: 23px;
    padding-bottom: 11px;
    background-color: #fff;
    border-top: 2px solid #999;
    margin-top: -2px;
}
.caret {
    display: inline-block;
    width: 0;
    height: 0;
    vertical-align: top;
    border-top: 4px solid #fff;
    border-right: 4px solid transparent;
    border-left: 4px solid transparent;
    content:"";
    margin-top: 15px;
    margin-left: 5px;
    border-bottom-color: #fff;
    filter: alpha(opacity=100);
    background-image:url(images/topnav-arrow-down-white-ie6.png)no-repeat 0 0 transparent;
    _display: inline;
    _zoom: 1;
    _width: 7px;
    _height: 4px;
    _margin-top: 8px;
    _margin-left: 5px;
    _line-height: 4px;
    _border: none;
    _vertical-align: baseline;
}
<div id="companymenu">
    <ul class="companymenuul">
        <li class="companymenuli aactive"><a class="alisting">Home</a></li>
        <li class="companymenuli"><a class="alisting">Product Categories<b class="caret"></b></a>
            <ul>
                <li>1</li>
                <li>2</li>
                <li>3</li>
            </ul>
        </li>
        <li class="companymenuli"><a class="alisting">Company Profile  <b class="caret"></b></a>
        </li>
        <li class="companymenuli"><a class="alisting">Contacts</a>
        </li>
    </ul>
</div>

我希望在悬停产品类别<ul>

时打开<li>

7 个答案:

答案 0 :(得分:6)

你正在寻找这样的东西吗? Fiddle

为您的子菜单添加一个类,并为其添加display:none;。在您悬停display:block;

之后

所以你有这样的事情:

HTML:

<li class="companymenuli"><a class="alisting">Product Categories  <b class="caret"></b></a>
    <ul class="submenu">
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</li>

CSS:

.submenu{
    display:none;
}

.companymenuli:hover > ul{
    display:block;
}

修改

要删除子弹,只需将list-style-type:none;添加到submenu li标记即可。在这里你的更新小提琴:

Updated Fiddle

答案 1 :(得分:2)

在现有的css中添加此css:

.companymenuli ul {
    display:none;
}

.companymenuli:hover ul {
    display:block;
}

那么它会起作用

答案 2 :(得分:0)

请尝试以下HTMLCSS

<li class="companymenuli"><a class="alisting">Product Categories  <b class="caret"></b></a>
                <ul class="ul">
                    <li>1</li>
                    <li>2</li>
                    <li>3</li>
                </ul>                       
</li>

<style>
.ul
{
    display:none;
}
.companymenuli:hover .ul
{
    display:block
}
</style>

答案 3 :(得分:0)

检查解决方案

http://jsfiddle.net/n7J8x/4/

默认隐藏子菜单

.submenu{
     display:none;
}

在主菜单悬停时打开子菜单

.companymenuul > li:hover ul{
     display: block;
}

答案 4 :(得分:0)

$("#cssmenu").menumaker({
    title: "Menu",
    breakpoint: 768,
    format: "multitoggle"
});
@import url(https://fonts.googleapis.com/css?family=Montserrat:400,700);
#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a,
#cssmenu #menu-button {
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  line-height: 1;
  display: block;
  position: relative;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
#cssmenu:after,
#cssmenu > ul:after {
  content: ".";
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
}
#cssmenu #menu-button {
  display: none;
}
#cssmenu {
  font-family: Montserrat, sans-serif;
  background: #1b9bff;
}
#cssmenu > ul > li {
  float: left;
}
#cssmenu.align-center > ul {
  font-size: 0;
  text-align: center;
}
#cssmenu.align-center > ul > li {
  display: inline-block;
  float: none;
}
#cssmenu.align-center ul ul {
  text-align: left;
}
#cssmenu.align-right > ul > li {
  float: right;
}
#cssmenu > ul > li > a {
  padding: 17px;
  font-size: 12px;
  letter-spacing: 1px;
  text-decoration: none;
  color: #eeeeee;
  font-weight: 700;
  text-transform: uppercase;
}
#cssmenu > ul > li:hover > a {
  color: #ffffff;
}
#cssmenu > ul > li.has-sub > a {
  padding-right: 30px;
}
#cssmenu > ul > li.has-sub > a:after {
  position: absolute;
  top: 22px;
  right: 11px;
  width: 8px;
  height: 2px;
  display: block;
  background: #eeeeee;
  content: '';
}
#cssmenu > ul > li.has-sub > a:before {
  position: absolute;
  top: 19px;
  right: 14px;
  display: block;
  width: 2px;
  height: 8px;
  background: #eeeeee;
  content: '';
  -webkit-transition: all .25s ease;
  -moz-transition: all .25s ease;
  -ms-transition: all .25s ease;
  -o-transition: all .25s ease;
  transition: all .25s ease;
}
#cssmenu > ul > li.has-sub:hover > a:before {
  top: 23px;
  height: 0;
}
#cssmenu ul ul {
  position: absolute;
  left: -9999px;
}
#cssmenu.align-right ul ul {
  text-align: right;
}
#cssmenu ul ul li {
  height: 0;
  -webkit-transition: all .25s ease;
  -moz-transition: all .25s ease;
  -ms-transition: all .25s ease;
  -o-transition: all .25s ease;
  transition: all .25s ease;
}
#cssmenu li:hover > ul {
  left: auto;
}
#cssmenu.align-right li:hover > ul {
  left: auto;
  right: 0;
}
#cssmenu li:hover > ul > li {
  height: 35px;
}
#cssmenu ul ul ul {
  margin-left: 100%;
  top: 0;
}
#cssmenu.align-right ul ul ul {
  margin-left: 0;
  margin-right: 100%;
}
#cssmenu ul ul li a {
  border-bottom: 1px solid rgba(150, 150, 150, 0.15);
  padding: 11px 15px;
  width: 170px;
  font-size: 12px;
  text-decoration: none;
  color: #eeeeee;
  font-weight: 400;
  background: #1b9bff;
}
#cssmenu ul ul li:last-child > a,
#cssmenu ul ul li.last-item > a {
  border-bottom: 0;
}
#cssmenu ul ul li:hover > a,
#cssmenu ul ul li a:hover {
  color: #ffffff;
}
#cssmenu ul ul li.has-sub > a:after {
  position: absolute;
  top: 16px;
  right: 11px;
  width: 8px;
  height: 2px;
  display: block;
  background: #eeeeee;
  content: '';
}
#cssmenu.align-right ul ul li.has-sub > a:after {
  right: auto;
  left: 11px;
}
#cssmenu ul ul li.has-sub > a:before {
  position: absolute;
  top: 13px;
  right: 14px;
  display: block;
  width: 2px;
  height: 8px;
  background: #eeeeee;
  content: '';
  -webkit-transition: all .25s ease;
  -moz-transition: all .25s ease;
  -ms-transition: all .25s ease;
  -o-transition: all .25s ease;
  transition: all .25s ease;
}
#cssmenu.align-right ul ul li.has-sub > a:before {
  right: auto;
  left: 14px;
}
#cssmenu ul ul > li.has-sub:hover > a:before {
  top: 17px;
  height: 0;
}
#cssmenu.small-screen {
  width: 100%;
}
#cssmenu.small-screen ul {
  width: 100%;
  display: none;
}
#cssmenu.small-screen.align-center > ul {
  text-align: left;
}
#cssmenu.small-screen ul li {
  width: 100%;
  border-top: 1px solid rgba(120, 120, 120, 0.2);
}
#cssmenu.small-screen ul ul li,
#cssmenu.small-screen li:hover > ul > li {
  height: auto;
}
#cssmenu.small-screen ul li a,
#cssmenu.small-screen ul ul li a {
  width: 100%;
  border-bottom: 0;
}
#cssmenu.small-screen > ul > li {
  float: none;
}
#cssmenu.small-screen ul ul li a {
  padding-left: 25px;
}
#cssmenu.small-screen ul ul ul li a {
  padding-left: 35px;
}
#cssmenu.small-screen ul ul li a {
  color: #eeeeee;
  background: none;
}
#cssmenu.small-screen ul ul li:hover > a,
#cssmenu.small-screen ul ul li.active > a {
  color: #ffffff;
}
#cssmenu.small-screen ul ul,
#cssmenu.small-screen ul ul ul,
#cssmenu.small-screen.align-right ul ul {
  position: relative;
  left: 0;
  width: 100%;
  margin: 0;
  text-align: left;
}
#cssmenu.small-screen > ul > li.has-sub > a:after,
#cssmenu.small-screen > ul > li.has-sub > a:before,
#cssmenu.small-screen ul ul > li.has-sub > a:after,
#cssmenu.small-screen ul ul > li.has-sub > a:before {
  display: none;
}
#cssmenu.small-screen #menu-button {
  display: block;
  padding: 17px;
  color: #eeeeee;
  cursor: pointer;
  font-size: 12px;
  text-transform: uppercase;
  font-weight: 700;
}
#cssmenu.small-screen #menu-button:after {
  position: absolute;
  top: 22px;
  right: 17px;
  display: block;
  height: 4px;
  width: 20px;
  border-top: 2px solid #eeeeee;
  border-bottom: 2px solid #eeeeee;
  content: '';
  box-sizing: content-box;
}
#cssmenu.small-screen #menu-button:before {
  position: absolute;
  top: 16px;
  right: 17px;
  display: block;
  height: 2px;
  width: 20px;
  background: #eeeeee;
  content: '';
  box-sizing: content-box;
}
#cssmenu.small-screen #menu-button.menu-opened:after {
  top: 23px;
  border: 0;
  height: 2px;
  width: 15px;
  background: #ffffff;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}
#cssmenu.small-screen #menu-button.menu-opened:before {
  top: 23px;
  background: #ffffff;
  width: 15px;
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
}
#cssmenu.small-screen .submenu-button {
  position: absolute;
  z-index: 99;
  right: 0;
  top: 0;
  display: block;
  border-left: 1px solid rgba(120, 120, 120, 0.2);
  height: 46px;
  width: 46px;
  cursor: pointer;
}
#cssmenu.small-screen .submenu-button.submenu-opened {
  background: #0190ff;
}
#cssmenu.small-screen ul ul .submenu-button {
  height: 34px;
  width: 34px;
}
#cssmenu.small-screen .submenu-button:after {
  position: absolute;
  top: 22px;
  right: 19px;
  width: 8px;
  height: 2px;
  display: block;
  background: #eeeeee;
  content: '';
}
#cssmenu.small-screen ul ul .submenu-button:after {
  top: 15px;
  right: 13px;
}
#cssmenu.small-screen .submenu-button.submenu-opened:after {
  background: #ffffff;
}
#cssmenu.small-screen .submenu-button:before {
  position: absolute;
  top: 19px;
  right: 22px;
  display: block;
  width: 2px;
  height: 8px;
  background: #eeeeee;
  content: '';
}
#cssmenu.small-screen ul ul .submenu-button:before {
  top: 12px;
  right: 16px;
}
#cssmenu.small-screen .submenu-button.submenu-opened:before {
  display: none;
}
#cssmenu.small-screen.select-list {
  padding: 5px;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- jQuery -->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<!-- MenuMaker Plugin -->
<script src="https://s3.amazonaws.com/menumaker/menumaker.min.js"></script>

<!-- Icon Library -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<meta charset="UTF-8">
<title>Title</title>
</head>

<body>
<div id="cssmenu">
  <ul>
     <li><a href="#" target="_blank"><i class="fa fa-fw fa-home"></i> Home</a></li>
     <li><a href="#"><i class="fa fa-fw fa-bars"></i> Product Categories</a>
        <ul>
           <li><a href="#">Menu 1</a>
              <ul>
                 <li><a href="#">Menu 1.1</a>
                    <ul>
                       <li><a href="#">Menu 1.1.1</a></li>
                       <li><a href="#">Menu 1.1.2</a></li>
                       <li><a href="#">Menu 1.1.3</a></li>
                    </ul>
                 </li>
                 <li><a href="#">Menu 1.2</a></li>
                 <li><a href="#">Menu 1.3</a></li>
              </ul>
           </li>
           <li><a href="#">Menu 2</a>
              <ul>
                 <li><a href="#">Menu 2.1</a></li>
                 <li><a href="#">Menu 2.2</a></li>
                 <li><a href="#">Menu 2.3</a></li>
              </ul>
           </li>
           <li><a href="#">Menu 3</a>
              <ul>
                 <li><a href="#">Menu 3.1</a></li>
                 <li><a href="#">Menu 3.2</a></li>
                 <li><a href="#">Menu 3.3</a></li>
              </ul>
           </li>
           <li><a href="#">Menu 4</a></li>
           <li><a href="#">Menu 5</a></li>
           <li><a href="#">Menu 6</a></li>
        </ul>
     </li>
     <li><a href="#"><i class="fa fa-fw fa-edit"></i> Company Profile</a></li>
     <li><a href="#"><i class="fa fa-fw fa-phone"></i> Contact</a></li>
  </ul>
</div>
</body>

</html>

答案 5 :(得分:-1)

完全解决answer fiddle。 我已经自己解决了。

感谢所有帮助我的人。

.companymenuli:hover > ul{
    display:block;
    background-color: #999;
    color:#FFF;
}
.submenu{
    display:none;
    position:absolute;
    z-index:1;
    width: 120px;
}
.submenu li{
    list-style-type:none;
}
.m-header {
    border: none;
    border-bottom: 2px solid #999;
    background-color: #fff;
    color: #333;
    font-size: 16px;
    width:100%;
    position:absolute;
}

答案 6 :(得分:-2)

public class QuizActivity extends AppCompatActivity {

    private ActionBarDrawerToggle mToggle;

    private QuestionLibrary mQuestionLibrary = new QuestionLibrary();

    private TextView mScoreView;
    private TextView mQuestionView;
    private Button mButtonChoice1;
    private Button mButtonChoice2;
    private Button mButtonChoice3;
    private String mAnswer;
    private int mScore = 0;
    private int mQuestionNumber = 0;
    Dialog dialog;
    Dialog dialog2;
    TextView closeButton;
    TextView closeButton2;
    CheckBox checkBoxmp;
    private MediaPlayer mp, mp2;

    SharedPreferences mypref;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);


        //Dialog 1
        createDialog();
        Button dialogButton = (Button) findViewById(R.id.dialogbtn);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();

            }
        });

        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //end Dialog 1

        //Dialog 2
        createDialog2();
        Button dialogButton2 = (Button) findViewById(R.id.dialogbtn2);
        dialogButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog2.show();

            }
        });


        closeButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog2.dismiss();
            }
        });
        //end Dialog 2

        SharedPreferences mypref = getPreferences(MODE_PRIVATE);

        final SharedPreferences.Editor editor = mypref.edit();

        checkBoxmp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                editor.putBoolean("playSounds", !isChecked);
                editor.commit();
                if (null != mp && null != mp2) {
                    if (!isChecked) {
                        mp.setVolume(1, 1);
                        mp2.setVolume(1, 1);
                    } else {
                        mp.setVolume(0, 0);
                        mp2.setVolume(0, 0);
                    }
                }
            }
        });

        final boolean playSounds = mypref.getBoolean("playSounds", false);
        checkBoxmp.setChecked(!playSounds);

        TextView shareTextView = (TextView) findViewById(R.id.share);
        shareTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(Intent.ACTION_SEND);
                myIntent.setType("text/plain");
                myIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello!");
                myIntent.putExtra(Intent.EXTRA_TEXT, "My highscore in Quizzi is very high! I bet you can't beat me except you are cleverer than me. Download the app now! https://play.google.com/store/apps/details?id=amapps.impossiblequiz");
                startActivity(Intent.createChooser(myIntent, "Share with:"));
            }
        });

        mQuestionLibrary.shuffle();

        setSupportActionBar((Toolbar) findViewById(R.id.nav_action));
        DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Able to see the Navigation Burger "Button"

        ((NavigationView) findViewById(R.id.nv1)).setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.nav_stats:
                        startActivity(new Intent(QuizActivity.this, Menu2.class));
                        break;
                    case R.id.nav_about:
                        startActivity(new Intent(QuizActivity.this, Menu3.class));
                        break;
                }
                return true;
            }
        });

        mScoreView = (TextView) findViewById(R.id.score_score);
        mQuestionView = (TextView) findViewById(R.id.question);
        mButtonChoice1 = (Button) findViewById(R.id.choice1);
        mButtonChoice2 = (Button) findViewById(R.id.choice2);
        mButtonChoice3 = (Button) findViewById(R.id.choice3);

        final List<Button> choices = new ArrayList<>();
        choices.add(mButtonChoice1);
        choices.add(mButtonChoice2);
        choices.add(mButtonChoice3);

        updateQuestion();


        //Code of the mediaplayer begins:

        for (final Button choice : choices) {
            choice.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (choice.getText().equals(mAnswer)) {
                        try {
                            mp = new MediaPlayer();
                            if (playSounds) {
                                mp.setVolume(1, 1);
                            } else {
                                mp.setVolume(0, 0);
                            }

                            AssetFileDescriptor afd;
                            afd = getAssets().openFd("sample.mp3");
                            mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                            mp.prepare();

                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                mp.release();
                            }
                        });
                        mp.start();
                        updateScore();
                        updateQuestion();
                        Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();

                    } else {
                        try {
                            mp2 = new MediaPlayer();
                            if (playSounds) {
                                mp2.setVolume(1, 1);
                            } else {
                                mp2.setVolume(0, 0);
                            }
                            AssetFileDescriptor afd;
                            afd = getAssets().openFd("wrong.mp3");
                            mp2.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                            mp2.prepare();

                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                mp.release();
                            }
                        });
                        mp2.start();

                        Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(QuizActivity.this, Menu2.class);
                        intent.putExtra("score", mScore); // pass score to Menu2
                        startActivity(intent);
                    }
                }
            });
        }
    }


    //End mediaplayer main code
    private void updateQuestion() {
        if (mQuestionNumber < mQuestionLibrary.getLength()) {
            mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
            mButtonChoice1.setText(mQuestionLibrary.getChoice1(mQuestionNumber));
            mButtonChoice2.setText(mQuestionLibrary.getChoice2(mQuestionNumber));
            mButtonChoice3.setText(mQuestionLibrary.getChoice3(mQuestionNumber));
            mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber++);
        } else {
            Toast.makeText(QuizActivity.this, "Last Question! You are very intelligent!", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(QuizActivity.this, Menu2.class);
            intent.putExtra("score", mScore);
            startActivity(intent);
        }
    }

    private void updateScore() {
        mScoreView.setText(String.valueOf(++mScore));

        SharedPreferences mypref = getPreferences(MODE_PRIVATE);
        int highScore = mypref.getInt("highScore", 0);

        if (mScore > highScore) {
            SharedPreferences.Editor editor = mypref.edit();
            editor.putInt("highScore", mScore);
            editor.apply();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return mToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
    }

    private void createDialog() {
        dialog = new Dialog(this);
        dialog.setTitle("Tutorial");
        dialog.setContentView(R.layout.popup_menu1_1);
        closeButton = (TextView) dialog.findViewById(R.id.closeTXT);
    }


    private void createDialog2() {
        dialog2 = new Dialog(this);
        dialog2.setTitle("Settings");
        dialog2.setContentView(R.layout.popup_menu1_2);
        closeButton2 = (TextView) dialog2.findViewById(R.id.closeTXT2);
        checkBoxmp = (CheckBox) dialog2.findViewById(R.id.ckeckBox);
    }
}