从中心用颜色填充圆形Div
Fill Circle-Shaped Div From Center with Color On Click
我有一个圆形的div, class=" complete-button" (height/width =27px, border radius = 12px)。我想在点击时改变这个div的背景颜色,从中心开始,用温和的放松向外扩展。
做这件事最有效的方法是什么?我已经看到一些帖子使用css on-hover(使用前后选择器)实现了这一点,但我还没有足够的悟性将这个想法转化为jQuery on-click。任何帮助将非常感激!
使用以下CSS:
div.button:after {
content: ''; /* needed for rendering */
position: relative;
display: block; /* so we can set width and height */
border-radius: 50%;
height: 0%;
width: 0%;
margin: auto; /* center horizontally */
background: red;
top: 50%; /* center vertically */
transform: translateY(-50%); /* center vertically */
transition: 1s;
}
div.button.selected:after {
height: 100%;
width: 100%;
}
然后切换selected
类点击。
$('div').click(function() {
$(this).toggleClass('selected');
});
div.button {
height: 27px;
width: 27px;
border-radius: 50%;
background: green;
}
div.button:after {
content: ''; /* needed for rendering */
position: relative;
display: block; /* so we can set width and height */
border-radius: 50%;
height: 0%;
width: 0%;
margin: auto; /* center horizontally */
background: red;
top: 50%; /* center vertically */
transform: translateY(-50%); /* center vertically */
transition: 1s;
}
div.button.selected:after {
height: 100%;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button">
</div>
你可以在css中定义你的过渡效果,并使用js来执行简单的类切换。
在codepen上播放
html:<button class="myButton">My Button</button>
css: .myButton{
margin-left:auto;
margin-right: auto;
display: block;
transition: background-color,height,width 0.5s ease;
background-color: red;
height: 100px;
width: 100px;
border-radius: 49px;
}
.myButton.on{
background-color: yellow;
height: 120px;
width: 120px;
border-radius: 59px;
}
JS:
$(function(){
$('.myButton').click(function(){
$(this).toggleClass('on');
});
})();
相关文章:
- 如何设置html元素填充的动画
- 如何使用jquery在填充自动完成的值后使文本框只读
- 如何通过ajax刷新JSF填充的javascript变量
- 添加文字和评论功能更新Div
- 用与线条相同的颜色填充多折线图上的点
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 隐藏 DIV 标签,直到填充所有文本字段
- 使用数据库中的数据从外部Javascript动态填充DIV
- 用另一个页面内容动态填充正确的DIV
- Div填充剩余高度
- 如何隐藏Div填充直到可见
- 从中心用颜色填充圆形Div
- 将Div设置为空,然后重新填充,因为我的页面会在所有元素中闪烁
- 幻灯片填充Div
- Div边框扩展Div的上下填充
- DIV背景定位与填充
- Div没有填充新内容
- 用JavaScript填充多个DIV
- 用星号填充DIV;待打印
- 如何使用Jquery和Dropdown填充Div