如何更改鼠标移动时的渐变程度
How to change degree of gradient on mouse movement?
我的灵感来自这个代码 sinppet 来自 http://codepen.io/andreasstorm/pen/syvoL
.HTML-
<header>
<div id="range"></div>
<div id="currentVal">background-image: linear-gradient( <span id="spanVal">180</span>deg , <span style="color:#4ac1ff">#4ac1ff</span>, <span style="color:#795bb0">#795bb0</span> )</div>
</header>
<div class="gradient"></div>
CSS (触笔) -
html
height 100%
body
background-color #292c2f
font-family monospace
overflow hidden
.gradient
height calc(100% - 70px)
background-image linear-gradient(180deg,#4ac1ff,#795bb0)
position absolute
width 100%
header
background #252525
height 70px
line-height 70px
#currentVal
color #424242
font-size 14px
font-weight 800
padding-left 240px
span
color #ccc
#range
width 180px
border 0
height 4px
background #424242
outline none
position absolute
left 30px
top 32px
.ui-slider-handle
position absolute
margin 0px 0 0 -7px
border-radius 100%
background white
border 0
height 14px
width 14px
outline none
cursor pointer
&:hover
&:focus
transform scale(1.1)
.ui-slider-range
background #4ac1ff
JavaScript (coffeeScript) -
$("#range").slider
range: "min"
max: 360
value: 180
slide: (e, ui) ->
$("#spanVal").html ui.value
bg = "linear-gradient(" + ui.value + "deg,#4ac1ff,#795bb0)"
$(".gradient").css "background-image", bg
以同样的方式,我试图在网页的背景上实现它,尽管我不太精通JavaScript。无论如何,我们可以对鼠标移动实现相同的效果吗?我的意思是更改渐变的程度以及光标在屏幕上的位置。
任何帮助将不胜感激!
这个呢?是你要找的吗?
$(".gradient").mousemove(function( event ) {
var w = $(this).width(),
pct = 360*(+event.pageX)/w,
bg = "linear-gradient(" + pct + "deg,#4ac1ff,#795bb0)";
$(".gradient").css("background-image", bg);
});
我只使用 x 运动。您可以使用您喜欢的内容。用户使用类 .gradient
在div 周围移动鼠标。我只是使用div
宽度将值调整为百分比并更改背景。
添加 x+y 并给出随机度
var originalBG = $(".gradient").css("background");
$('.gradient')
.mousemove(function(e) {
x = e.pageX - this.offsetLeft;
y = e.pageY - this.offsetTop;
xy = x + y;
bgWebKit = "linear-gradient(" + xy + "deg, #4ac1ff, #795bb0)";
$(this).css({
'background': bgWebKit
})
}).mouseleave(function() {
$(this).css({
background: originalBG
});
});
* {
margin: 0;
padding: 0;
}
html {
height: 100%;
}
body {
background-color: #292c2f;
font-family: monospace;
overflow: hidden;
}
.gradient {
height: calc(100%);
background: -webkit-linear-gradient(270deg, #4ac1ff, #795bb0);
background: -moz-linear-gradient(270deg, #4ac1ff, #795bb0);
background: -o-linear-gradient(270deg, #4ac1ff, #795bb0);
background: -ms-linear-gradient(270deg, #4ac1ff, #795bb0);
background: linear-gradient(180deg, #4ac1ff, #795bb0);
position: absolute;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="gradient"></div>
相关文章:
- Highcharts colorsByPoint与系列中的线性渐变一起使用时不起作用
- 在SVG地图上添加水的渐变,就像在谷歌地图(PHP/JS)中一样
- 画布使用带有角度的线性渐变背景集
- 在文本区域的焦点上分割渐变
- Chart.js-添加渐变而不是纯色-实现解决方案
- 简单的jQuery段落渐变器
- 猫头鹰旋转木马2罐头't使渐变动画工作
- 颜色滑块渐变
- jQuery鼠标悬停渐变效果
- jQuery渐变光晕特殊文本效果
- 全宽滑块-交叉渐变过渡问题
- Cufon..简单渐变文本..是't工作
- 如何在three.js中实现不同相机/场景之间的交叉渐变
- 如何在android中使用HTML/CSS赋予文本渐变颜色
- 一个定时器事件会在多大程度上减慢我在JavaScript中的代码
- 使用像素操作修改画布在某种程度上不会;我不能完全在IE9中工作
- TypeError:熟练程度不是一个函数,但它确实是
- JavaScript应用CSS3渐变
- 我怎么能有一个自动渐变背景使用JavaScript,它适用于最新版本的Internet Explorer
- 如何更改鼠标移动时的渐变程度