Home > Article > Web Front-end > How to use CSS technology to achieve cool special effects in drop-down boxes
This article introduces you to a cool drop-down box implemented using CSS. The effect after implementation is really very good. The detailed implementation process and sample code are given in the article. Interested friends can take a look below. Take a look.
First let’s take a look at the renderings to be achieved
I want to make such a The effect is still cumbersome, but the code is not difficult to understand.
First, let’s take a look at the Html code.
##XML/HTML CodeCopy content to clipboard
<p class="container">
<p class="heading">
<h2>Custom Selecth2> ;
p>
p class="select">## <
>Please select p> ## <li data-value= HTML5li># <li data-value="CSS3"
li ># <li data-value="JavaScript"
>li>
li>## <li data -value="Backbone">Backbone
> ;/p>##
p
It can be seen that we do not use the native select element, but use other elements to simulate this effect. We specified data-value for the li element, mainly because we will use JQuery to get the selected value and place it under the p element.
Let’s look at the CSS code step by step.
##CSS CodeCopy content to clipboard
: 0;
: 0;
'Terminal';
}
#33CC66;
}
XML/HTML Code
Copy content to clipboard
link href='http://fonts.googleapis.com/css?family=Lobster|Terminal+Dosis' rel='stylesheet' type='text/css'>We used the Terminal font above, and we will also use the Lobster font next, so use this line of code to add a reference.
CSS Code
Copy content to clipboard
.select > p, .select ul {## ##font-size
: 2rem;border: 1px
;
## }
##.select > p {## position
:right-radius: 0;
color: rgba(102, 102, 102, .6);
#.select > p:after {block
;width: 10px
;height: 10px
;content: ''
; ##; ##: 2rem;## # transform: rotate(-45deg);
## transition: transform .3s ease-out,} }
2. Specify a separate style for the p element and set its position attribute, mainly to draw the drop-down button on the right side below.
3. Use :after to draw the drop-down button on the right side of the p element. It can be seen that we use the lower left border and then rotate -45 degrees to simulate this effect. It is worth noting that we need to set its display to block and set the width and height, otherwise we will not see this effect.CSS Code
Copy content to clipboard.select ul { margin-top: 0;
border-top-left -radius: 0;
border-toplist-style-type
;
##cursor
:pointer
;
-y: auto;
: 0;
transition: max-height .3s ease-out;
}
.select ul li { padding -left: 0.5rem;
display: block;
##line-height: 3rem;
left;
}1. Set some default attributes of ul, set the maximum width to 0, and specify overflow-y as auto. At this time, ul will be hidden.
2. I encountered a problem when setting here, that is, the li tag always occupies a line that is not full of ul. That is because it has margin and padding by default, so at the beginning, all the pages in the web page are included. The element's margins and padding are set to 0.CSS CodeCopy content to clipboard
.select.open ul {
max-height: 20rem;
transform-origin: 50% 0;
-webkit-animation: slide-down .5s ease-in;
}
.select.open > p:after {
position: absolute;
top: 1.6rem;
transform: rotate(-225deg);
transition : transform .3s ease-in, top .2s ease-in;
}
1, is open Set the maximum height and animate it.
2. Rotate the drop-down button -225 degrees and assign animation to it.
Let’s take a look at the slide-down animation effect specified for the ul element, which is also the key to this drop-down effect.
CSS CodeCopy content to clipboard
@-webkit- keyframes slide-down {
0% {
transform: scale(1, 0);
}
There are some simple CSS codes below, which will not be explained again.
CSS Code
Copy content to clipboard
.select ul li :hover {
: rgba(102, 153, 102, 0.4);
}
CSS Now that we’ve finished talking, let’s take a look at how we use JQuery control this effect.
<script
"http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js" >script>##Now you can use JQuery .
XML/HTML CodeCopy content to clipboard
<script>
$(document).ready(function () {
$('.select ul li').on("click", function (e) {
var _this = $(this);
$('.select >p').text(_this.attr('data-value'));
$(_this).addClass('selected').siblings().removeClass('selected');
$('.select').toggleClass('open');
cancelBubble(e);
});
$('.select>p').on("click", function (e) {
$('.select').toggleClass('open');
cancelBubble(e);
});
$(document).on('click', function () {
$('.select').removeClass('open');
})
})
function cancelBubble(event) {
if (event.stopPropagation) {
event.preventDefault();
event.stopPropagation();
} else {
event.returnValue = false;
event.cancelBubble();
}
}
script>
1、首先为 p 标签绑定 click 事件,在触发的时候,为 select 添加或移除 open class, 也就是将 ul 显示出来。
2、为 li 绑定 click 事件,当选中了一个 li 元素的时候,首先获取到 data-value,然后将其赋值给 p 标签,然后为选中的 li 添加 selected class,与此同时利用 siblings() 方法,让兄弟节点的 selected class 移除。
3、为 document 设置click 事件,当我们点击网页中任何一处地方的时候,如果 ul 是打开的,就将其关闭,不过这个时候由于所有元素都在 document 内,所以我们需要阻止事件冒泡,调用自己写的 cancelBubble() 方法。
总结
好了,本文的内容到这就基本介绍了,希望能对大家的学习或者工作带来一定的帮助,如果有疑问大家可以留言交流。
The above is the detailed content of How to use CSS technology to achieve cool special effects in drop-down boxes. For more information, please follow other related articles on the PHP Chinese website!