【问题标题】:Floating Button with context menu and form on demand带有上下文菜单和按需表单的浮动按钮
【发布时间】:2017-10-16 04:46:33
【问题描述】:

我正在尝试创建将浮动在内容之上的 html 表单。按钮将浮动在内容上,单击每个按钮时,下面会出现不同的 div 表单。类似于下面的屏幕截图。它仅在第一次出现时显示按钮,并且在单击表单下方的每个按钮时应附加并出现。如果是最后一个按钮,应该会出现带有附加按钮的上下文菜单。

我尝试过创建按钮和表单,但是如何:

1) 为 button1、button2 添加按钮类型的外观 ...这是 div

2) 单击时在最后一个按钮上显示上下文菜单

3) 点击相应按钮时隐藏和显示适当的表单

jsbin

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

#content {
  width: 100%;
  height: 100%;
  background-color: #FBBC06;
}

#form {
  width: 300px;
  background-color: rgba(255, 255, 255, 0.65);
  height: 50px;
  position: absolute;
  top: 36px;
  right: 168px;
  border: 1px solid darkred;
  border-radius: 22px;
}

.widthHeight {
  width: 100%;
  height: 100%;
}
#button1 {
  background-color: aquamarine;
  height: 50px;
  width: 20%;
  border-radius: 25px 0px 0px 25px;
  border: 1px solid blue;
  display: inline-block;
}
#button2 {
  height: 50px;
  width: 20%;
  background-color: aquamarine;
  display: inline-block;
}

#button3 {
  height: 50px;
  width: 20%;
  background-color: aquamarine;
  display: inline-block;
  border: 1px solid blue;
}

#button4{
  height: 50px;
  width: 20%;
  background-color: aquamarine;
  display: inline-block;
  border: 1px solid blue;
}
#button5 {
  height: 50px;
  width: 12%;
  background-color: aquamarine;
  display: inline-block;
  border-radius: 0px 25px 25px 0px;
  border: 1px solid blue;
}

#form1{
  width: 100%;
  height: 200px;
  border: 1px solid yellow;
  background-color: skyblue;
  margin-top: 13px;
}

#form2 {
  width: 100%;
  height: 200px;
  border: 1px solid yellow;
  background-color: skyblue;
  margin-top: 13px;
}

#form3 {
  width: 100%;
  height: 200px;
  border: 1px solid yellow;
  background-color: skyblue;
  margin-top: 13px;
}
<div class="widthHeight">
    <div id="content">
    </div>
    <div id="form">
        <div id="buttonHolder">
            <div id="button1">
                button1
            </div>
            <div id="button2">
                buton2
            </div>
            <div id="button3">
                button3
            </div>
            <div id="button4">
                button4
            </div>
            <div id="button5">
                button5
            </div>
        </div>
        <div id="form1">
            This is form 1 on click of button 1 it appears
        </div>
        <div id="form2">
            This is form 1 on click of button 1 it appears
        </div>
        <div id="form3">
            This is form 1 on click of button 1 it appears
        </div>
    </div>
</div>

【问题讨论】:

  • 您应该查找 css display: none。将它与display: block 结合使用应该很容易得到你想要的。
  • 我应该在按钮中添加什么 CSS 以使其看起来像可点击的按钮。它现在只是 div
  • 好吧,我所说的 css 是您将使用 javascript 来触发 css 更改(为了简单起见,使用 jquery,不是必需的),例如$("#button1").click( () =&gt; $("#form1").css('display', 'block') )如果你还不明白,我回家再看看。

标签: javascript jquery html css


【解决方案1】:

1) 个性化 CSS - 取决于您的个人喜好。更改背景颜色和/或光标可能会添加您需要的效果,所以像

.divbutton:hover
{
    cursor:pointer;
}

应该可以帮助你。 (将class='divbutton' 添加到作为按钮的每个 div 中)

3) 创建这些样式类

.hiddenitem
    {
      display:none;
    }

.shownitem
    {
      display:block;
    }

在每个表单中添加.hidenitem

<div id="form1" class="hiddenitem">
    This is form 1 on click of button 1 it appears 
</div>

在您的脚本文件或标签中

activateMenu = function (menuId)
{
     $("#form" + menuId).toggleClass('shownitem');
}

最后将onclick='activateMenu(menuId)'添加到每个对应的按钮

<div id="button2" onclick='activateMenu(2)'>
                                    TEXT1
</div>

对于 2) 使用与表单相同的方法:在 html 中创建上下文菜单,应用 .hidenitem 类并创建一个切换函数来切换菜单上的 .shownitem 类,使用它自己的 id 作为选择器。

编辑:JSFiddle

【讨论】:

  • 另外我不能给按钮那种效果,我应该添加什么 css 看起来 div 可以按。
  • 添加了小提琴。这在很大程度上取决于您和一个设计问题。这可以帮助你dabuttonfactory.com(嵌入css)
  • 谢谢,你的jsfiddle能让这个看起来更好吗
  • 也许,但如果这回答了你的问题,你应该接受它;)
  • 更新了 JSFiddle 链接,具有更好的可视化界面,现在链接到 Bootstrap cdn。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
  • 2014-05-04
  • 2013-07-06
相关资源
最近更新 更多