简体   繁体   English

在鼠标上使用jQuery从左至右背景位置输入幻灯片

[英]on mouse enter slide from left to right background position with jQuery

I have a button for which I want to slide his background from #000 to #ccc . 我有一个按钮,要将其背景from #000 to #ccc My problem is how to do that slide transition of button background on hover or mouseenter. 我的问题是如何在悬停或mouseenter上执行按钮背景的幻灯片过渡。 It is possible to do this with jQuery? 可以用jQuery做到这一点吗? Very important , I don' want to use background images so all suggestions about images ..skip pls. 非常重要,我不想使用背景图片,因此所有有关图片的建议都请跳过。

This is an annoying fiddle created only with css but is working only from top to bottom and not from left to right how I need. 这是仅用css创建的烦人的提琴,但仅从上到下而不是从左到右工作。 http://jsfiddle.net/xu3ck/166/ http://jsfiddle.net/xu3ck/166/

<a href="#" class="btn two">Submit Form</a>

.btn {
    width: 180px;
    text-decoration: none;
    height: 40px;
    border-radius: 10px;
    text-align: center;
    color: white;
    line-height: 40px;
    font-size: 20px;
    font-family: arial, sans-serif;
    margin: 20px;
    float: left;
    display: block;
     color: white;
    box-shadow: rgba(0,0,0,0.3) 1px 1px 3px inset;
}



.two {
    background: linear-gradient(#111, #eee);
    background-repeat: repeat;
    background-size: 100% 200%;
    transition: all .5s linear;
}

.two:hover, .two:focus, .two:active {
    background-position: 0 -200%;
}

ty. TY。

What the example does is it creates a liniar background on the button which spans 200% of the buttons height. 该示例所做的是,它在按钮上创建了一个线性背景,该背景跨越了按钮高度的200%。 When your mouse goes over the button it moves the background 200% up. 当鼠标移到该按钮上时,它将背景向上移动200%。
What I changed, is I changed the gradient from "top to bottom" to "left to right". 我所更改的是将渐变从“顶部到底部”更改为“左至右”。 Set the width 200% instead of the height, and on the hover I set the background 200% to the right. 将宽度设置为200%,而不是高度,在悬停时,我将背景设置为右侧200%。

I fixed it in this JsFiddle 我在这个JsFiddle中修复了它

What I changed was the following: 我更改的内容如下:

background: linear-gradient(#111, #eee);
To
background: linear-gradient(to right, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);

background-size: 100% 200%;
To
background-size: 200% 100%;

And
background-position: 0 -200%;
To
background-position: -200% 0;

You can make it work with :before and :after pseudoelements, which are compatible with IE8. 您可以使用:before:after伪元素(与IE8兼容)来使其工作。 No need for jQuery. 不需要jQuery。 Just CSS. 只是CSS。

See this demo here . 在这里查看此演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM