【问题标题】:HTML Material Design ButtonHTML 材料设计按钮
【发布时间】:2017-10-15 13:07:51
【问题描述】:

我想为组织的 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>

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

请帮帮我。

【问题讨论】:

    标签: html css button material-design


    【解决方案1】:

    通过链接卡片,我相信您的意思是说您希望卡片在点击链接时弹出。这可以使用 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>
    

    【讨论】:

    • 很好,但是,有没有办法在页面中间打开卡片,使其他所有内容变暗并且只使卡片可点击?
    • 抱歉,您一定误解了我的意思,抱歉,我的意思是它在整个页面的中间打开,因此您只能点击“完成”。此外,您能否将英语语言链接到卡片的副本,但使用随机文本,以便我可以告诉如何将卡片单独链接到每个按钮?谢谢
    • 我的错,我上次忘记保存我的编辑了。我再次更新了新的要求。希望这能解决一切。
    • 谢谢!从 stackexchange 看起来不错,但这是最重要的问题。如何将其添加到我的代码中?听起来可能很愚蠢(抱歉),但主代码应该是这样的:pastebin.com/AZEUcF7h 与 button.js 为:pastebin.com/q19BgJqp 如果需要,您能否添加单独的 HTML 代码和 js 并使用 pastebin 发送链接?谢谢
    • 您以前从未将外部文件链接到您的 html 吗?其实很简单。样式表和 JS 文件内容将与 sn-p 相同。您唯一需要做的就是在 html head 中添加文件。索引页:pastebin.com/1qFTcxe2,样式表:pastebin.com/x3KN0ByG。 JS:pastebin.com/jZDtNCLH
    猜你喜欢
    • 2016-06-28
    • 2015-01-04
    • 2016-09-20
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    相关资源
    最近更新 更多