[英]How to use margin:0 auto in jquery?
I tried to center align a div
on click, but this is not working 我试图使div
居中对齐,但这不起作用
$().click(function(){
$(".scale_roll").css({"width" : "80% ","margin":"0 auto"});
});
When I used marign-left
: 100px
it works fine. 当我使用marign-left
: 100px
它工作正常。 What is the problem in aligning the div to center using this property in jQuery? 在jQuery中使用此属性将div居中对齐有什么问题?
TYPO. 错字
auto 0
should be 0 auto
: auto 0
应该是0 auto
:
$(".scale_roll").css({"width" : "80% ","margin":"0 auto"});
Move div to the center and then back to its original position. 将div移到中心,然后回到其原始位置。 Please click " Run code snippet " below 请点击下面的“ 运行代码段 ”
$(".scale_roll").click(function(){ if($(this).width() == 100) $(this).css({"width" : "80% ","margin":"0 auto"}); else $(this).css({"width" : "100px ","margin":"0"}); });
.scale_roll{ width:100px; height:100px; background-color:green; color:#fff; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="scale_roll">Click Me<div>
You have not spelled your click
function correctly. 您尚未正确拼写您的click
功能。
$().click(function(){
$(".scale_roll").css({"width" : "80% ","margin":"0 auto"});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.