HTML Material 设计按钮

标签 html css button material-design

我想为某个组织的 GCSE 选项创建一个网站,目前我正在使用 Notepad++ 起草该网站。我在这方面有些新手。

我想创建一个可悬停的下拉菜单,其中列出了所有选项,如果单击,它会打开一张有关该主题的卡片。我喜欢 Material Design 概念,这是代码的一部分:

/* Style The Dropdown Button */

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}


/* The container <div> - needed to position the dropdown content */

.dropdown {
  position: relative;
  display: inline-block;
}


/* Dropdown Content (Hidden by Default) */

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}


/* Links inside the dropdown */

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}


/* Change color of dropdown links on hover */

.dropdown-content a:hover {
  background-color: #f1f1f1
}


/* Show the dropdown menu on hover */

.dropdown:hover .dropdown-content {
  display: block;
}


/* Change the background color of the dropdown button when the dropdown content is shown */

.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>

<div class="dropdown">
  <button class="dropbtn">Compulsary</button>
  <div class="dropdown-content">
    <a href="#">English Language</a>
    <a href="#">English Literature</a>
    <a href="#">Mathematics</a>
  </div>
</div>

然后,我还想将每个主题链接到这种卡片: https://getmdl.io/components/index.html#cards-section

我制作了一张样本卡:

.demo-card-wide.mdl-card {
  width: 512px;
}

.demo-card-wide>.mdl-card__title {
  color: #fff;
  height: 176px;
  background: url('https://www.tes.com/sites/default/files/maths_blackboard.jpg') center / cover;
}

.demo-card-wide>.mdl-card__menu {
  color: #fff;
}
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>

<div class="demo-card-wide mdl-card mdl-shadow--2dp">
  <div class="mdl-card__title">
    <h2 class="mdl-card__title-text">Mathematics</h2>
  </div>
  <div class="mdl-card__supporting-text">
    This is one of the compulsary subjects and is crucial. It involves geometry, algebra, data, and numbers.
  </div>
  <div class="mdl-card__actions mdl-card--border">
    <a class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
      Done
    </a>
  </div>
</div>

如何将卡片链接到下拉选项之一,以便它打开,以及如何链接卡片上的“完成”按钮以关闭卡片?

请帮我解决这个问题。

最佳答案

通过链接卡片,我相信您的意思是希望在单击链接时弹出卡片。这可以使用 javascript/jquery 轻松实现。纯粹的“hacky”CSS 解决方案也是可能的,但不推荐。

以下解决方案使用 jQuery

$(document).ready(function() {
  $(".dropdown-content a").click(function() {
    $(".background-mask").show();
    $(".demo-card-wide.mdl-card").show();

    $(".mdl-card__supporting-text").text(
      $(this).attr("data-content")
    );

    $(".demo-card-wide>.mdl-card__title").css('background', 'url(' + $(this).attr("data-img") + ') center / cover')
  });

  $(".mdl-button").click(function() {
    $(".demo-card-wide.mdl-card").hide();
    $(".background-mask").hide();
  });

});
/* Style The Dropdown Button */

body {
  position: relative;
}

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}


/* The container <div> - needed to position the dropdown content */

.dropdown {
  position: relative;
  display: inline-block;
  z-index: 0;
}


/* Dropdown Content (Hidden by Default) */

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}


/* Links inside the dropdown */

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}


/* Change color of dropdown links on hover */

.dropdown-content a:hover {
  background-color: #f1f1f1
}


/* Show the dropdown menu on hover */

.dropdown:hover .dropdown-content {
  display: block;
}


/* Change the background color of the dropdown button when the dropdown content is shown */

.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}

.demo-card-wide.mdl-card {
  width: 512px;
  display: none;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.demo-card-wide>.mdl-card__title {
  color: #fff;
  height: 176px;
}

.demo-card-wide>.mdl-card__menu {
  color: #fff;
}

.background-mask {
  display: none;
  position: absolute;
  width: 100vw;
  height: 100vh;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.7);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>

<div class="dropdown">
  <button class="dropbtn">Compulsary</button>
  <div class="dropdown-content">
    <a href="#" data-content="English Language Content" data-img="https://www.tes.com/sites/default/files/maths_blackboard.jpg">English Language</a>
    <a href="#" data-content="English Literature Content" data-img="https://images7.content-hcs.com/commimg/myhotcourses/blog/post/myhc_24232.jpg">English Literature</a>
    <a href="#" data-content="Mathematics Content" data-img="https://c.tadst.com/gfx/750w/english-language-day.jpg">Mathematics</a>
  </div>
</div>

<div class="background-mask"></div>

<div class="demo-card-wide mdl-card mdl-shadow--2dp">
  <div class="mdl-card__title">
    <h2 class="mdl-card__title-text">Mathematics</h2>
  </div>
  <div class="mdl-card__supporting-text">
    This is one of the compulsary subjects and is crucial. It involves geometry, algebra, data, and numbers.
  </div>
  <div class="mdl-card__actions mdl-card--border">
    <a class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
      Done
    </a>
  </div>
</div>

更新:

使用数据属性将相关内容添加到可插入卡片的链接中。在本例中,我添加了以下内容:

<a href="#" data-content="English Language Content" data-img="https://www.tes.com/sites/default/files/maths_blackboard.jpg">English Language</a>
<a href="#" data-content="English Literature Content" data-img="https://images7.content-hcs.com/commimg/myhotcourses/blog/post/myhc_24232.jpg">English Literature</a>
<a href="#" data-content="Mathematics Content" data-img="https://c.tadst.com/gfx/750w/english-language-day.jpg">Mathematics</a>

关于HTML Material 设计按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46755365/

相关文章:

单击 fragment 中的按钮时,Android 应用程序崩溃

java - 如何在 JavaFX 按钮上设置工具提示?

javascript - 创建具有相同样式但内容不同的多个模态窗口?

javascript - 计算给定字符串的真实像素宽度

html - 在 X 浏览器宽度下折叠导航栏@media 查询不起作用

css - -webkit-transition-timing-function 在 Chrome 中没有任何效果

javascript - 到达页面开头时Jquery淡入div

php - 删除 SQL 表中所有用户的按钮

html - 三个具有自动宽度的 div 与最宽的 div 一样宽,使每个 div 与其内容一样宽

Javascript 函数在 IE11 中未定义