CSS 过渡渐变为纯色

发布于 2024-12-17 23:30:19 字数 2889 浏览 2 评论 0原文

我目前在创建从渐变到纯色的过渡方面非常不成功。此外,由于我的渐变,我似乎看不到与右侧对齐的背景图像。

这是我的代码:

input {
    width: 306px;
     height:27px;
    font-size:12px;
    font-family: Arial,Helvetica,sans-serif;
    padding: 1px 30px 1px 9px;
    color: #767676;
    font-weight: normal;
    margin: 3px 0;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
     border-radius: 3px;
    border:1px solid #e5e5e5;
    background-image:url('http://i.imgur.com/9hdsg.png');
    background-position:right;
    background-repeat:no-repeat;

    -moz-transition-property: width, background-color;
  -webkit-transition-property: width, background-color;
  -o-transition-property: width, background-color;
  transition-property: width, background-color;

  -moz-transition-duration: 0.5s, 0.5s;
  -webkit-transition-duration: 0.5s, 0.5s;
  -o-transition-duration: 0.5s, 0.5s;
  transition-duration: 0.5s, 0.5s;




    background: #787878; /* Old browsers */
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzc4Nzg3OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjUwJSIgc3RvcC1jb2xvcj0iIzc4Nzg3OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjUxJSIgc3RvcC1jb2xvcj0iIzY4Njg2OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiM3OTc5NzkiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(top,  #787878 0%, #787878 50%, #686868 51%, #797979 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#787878), color-stop(50%,#787878), color-stop(51%,#686868), color-stop(100%,#797979)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  #787878 0%,#787878 50%,#686868 51%,#797979 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  #787878 0%,#787878 50%,#686868 51%,#797979 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  #787878 0%,#787878 50%,#686868 51%,#797979 100%); /* IE10+ */
background: linear-gradient(top,  #787878 0%,#787878 50%,#686868 51%,#797979 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#787878', endColorstr='#797979',GradientType=0 ); /* IE6-8 */


  }



input:focus {
  width: 535px;
  background-color:#ffffff;
}

这是一个例子: http://jsfiddle.net/8neF9/2/

谢谢大家:)

I'm quite unsuccessful currently at creating a transition from a gradient to a solid color. Additionally, because of my gradient, I cannot seem to see my background image that's aligned to the right side.

Here's my code:

input {
    width: 306px;
     height:27px;
    font-size:12px;
    font-family: Arial,Helvetica,sans-serif;
    padding: 1px 30px 1px 9px;
    color: #767676;
    font-weight: normal;
    margin: 3px 0;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
     border-radius: 3px;
    border:1px solid #e5e5e5;
    background-image:url('http://i.imgur.com/9hdsg.png');
    background-position:right;
    background-repeat:no-repeat;

    -moz-transition-property: width, background-color;
  -webkit-transition-property: width, background-color;
  -o-transition-property: width, background-color;
  transition-property: width, background-color;

  -moz-transition-duration: 0.5s, 0.5s;
  -webkit-transition-duration: 0.5s, 0.5s;
  -o-transition-duration: 0.5s, 0.5s;
  transition-duration: 0.5s, 0.5s;




    background: #787878; /* Old browsers */
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzc4Nzg3OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjUwJSIgc3RvcC1jb2xvcj0iIzc4Nzg3OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjUxJSIgc3RvcC1jb2xvcj0iIzY4Njg2OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiM3OTc5NzkiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(top,  #787878 0%, #787878 50%, #686868 51%, #797979 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#787878), color-stop(50%,#787878), color-stop(51%,#686868), color-stop(100%,#797979)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  #787878 0%,#787878 50%,#686868 51%,#797979 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  #787878 0%,#787878 50%,#686868 51%,#797979 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  #787878 0%,#787878 50%,#686868 51%,#797979 100%); /* IE10+ */
background: linear-gradient(top,  #787878 0%,#787878 50%,#686868 51%,#797979 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#787878', endColorstr='#797979',GradientType=0 ); /* IE6-8 */


  }



input:focus {
  width: 535px;
  background-color:#ffffff;
}

Here's an example:
http://jsfiddle.net/8neF9/2/

Thanks everybody :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦旅人picnic 2024-12-24 23:30:19
  1. 要使多个背景正常工作,您需要在一行中声明它们:
    background: url(path/to/image), Linear-gradient(...) - 第一个是最接近的,所以它覆盖了其他的。
  2. 要将渐变动画设置为纯色,请将渐变设置为元素的两倍高(或长),因此 50% 使用 25% 等。
  3. 背景位置也声明为一行,重复也是如此。对于您的情况:

    背景大小:27px 29px,100% 200%; // 第一个图像,然后梯度两倍高
    背景重复:不重复、重复; // 不重复图像,重复渐变
    
  4. 将渐变动画设置为纯色 - 使用过渡将背景位置从:background-position: 0 50%, 0 0; 更改为background-position:0 50 %, 0 100%;

我已经更新了你的小提琴,但没有尝试让它与 ie 过滤器或旧的 webkit 一起使用句法。

  1. for multiple backgrounds to work you need to declare them in one line:
    background: url(path/to/image), linear-gradient(...) - first is the closest so it covers others.
  2. to animate gradient to solid color, make a gradient twice as high (or long) as the element, so instead 50% use 25% etc.
  3. background positions are also declared to in one line, same goes for repeat. In your case:

    background-size: 27px 29px, 100% 200%; // first image, then gradient twice as high
    background-repeat: no-repeat, repeat; // don't repeat image, repeat gradient
    
  4. to animate gradient to solid color - use transition to change background position from: background-position: 0 50%, 0 0; to background-position:0 50%, 0 100%;

I've upadted your fiddle, but didn't try to make it work with ie filter or old webkit syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文