Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
668 views
in Technique[技术] by (71.8m points)

html - Add box shadow with transition effect

I'm adding a class to a div that adds a box shadow to that div. This happens dynamically via jquery. Now, when the class is added, the shadow effect is added automatically as well, without any effect. Is there a way to add some transition effect via css in this case?

HTML:

<div id="box"></div>

CSS:

#box {
    width: 50px;
    height: 200px;
}

.shadow {
    -webkit-box-shadow: 0px 0px 4px 2px #D50E0E;
    -moz-box-shadow: 0px 0px 4px 2px #D50E0E;
    box-shadow: 0px 0px 4px 2px #D50E0E;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, simply add the transition (or the vendor-prefixed versions) to the CSS:

$('#t').click(
  function(){
    $('#box').toggleClass('shadow');
  });
#box {
  width: 50px;
  height: 200px;
  -webkit-transition: all 1s linear;
  -o-transition: all 1s linear;
  -moz-transition: all 1s linear;
  -ms-transition: all 1s linear;
  -kthtml-transition: all 1s linear;
  transition: all 1s linear;
}

.shadow {
  -webkit-box-shadow: 0px 0px 4px 2px #D50E0E;
  -moz-box-shadow: 0px 0px 4px 2px #D50E0E;
  box-shadow: 0px 0px 4px 2px #D50E0E;
  -webkit-transition: all 1s linear;
  -o-transition: all 1s linear;
  -moz-transition: all 1s linear;
  -ms-transition: all 1s linear;
  -kthtml-transition: all 1s linear;
  transition: all 1s linear;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="t">Toggle the 'shadow' class</button>

<div id="box">Some content in the 'box' div.</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...