整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

css 渐变跟随鼠标光标按钮动画

css 渐变跟随鼠标光标按钮动画

何使用css实现渐变跟随鼠标光标的悬停效果。如下图:

实现思路:

  1. 声明 --x 和- -y 两个 CSS 变量,用于跟踪鼠标在按钮上的位置。
  2. 声明一个 --size 的 CSS 变量,用于修改背景渐变的尺寸大小。
  3. 使用 background: radial-gradient(circle closest-side, pink, transparent) 创建按钮的径向渐变背景效果。
  4. 使用document.querySelector()和EventTarget.addEventListener()给按钮绑定'mousemove'事件。
  5. 使用element.getBoundingClientRect()和CSSStyleDeclaration.setProperty() 更新 --x 和--y 变量的值。

html:

<button class="mouse-cursor-gradient-tracking">
  <span>Hover me</span>
</button>

css:

/*按钮基本样式*/
.mouse-cursor-gradient-tracking {
  position: relative;
  background: #7983ff;
  padding: 0.5rem 1rem;
  font-size: 1.2rem;
  border: none;
  color: white;
  cursor: pointer;
  outline: none;
  overflow: hidden;
}
.mouse-cursor-gradient-tracking span {
  position: relative;
}
/*按钮渐变背景,这里使用伪类实现,并且使用transform动画*/
.mouse-cursor-gradient-tracking:before {
  --size: 0; /*渐变背景尺寸*/
  content: '';
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: var(--size);
  height: var(--size);
  /*背景渐变*/
  background: radial-gradient(circle closest-side, pink, transparent);
  /*动画效果*/
  transform: translate(-50%, -50%); 
  transition: width 0.2s ease, height 0.2s ease;
}
/*鼠标经过按钮时*/
.mouse-cursor-gradient-tracking:hover:before {
  --size: 200px;
}

javascript:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			#box{
				width: 300px;
				height: 300px;
				border: 2px solid black;
				background-color: yellow;
			}
		</style>
		<script>

			var w;
			var h;
			// currentStyle IE
			var i=1;
			var j=1;
			var c=0;
			function cool(name,value){
				var odiv=document.getElementById('box');
				var obj_w=getComputedStyle(odiv,false).width;
				var obj_h=getComputedStyle(odiv,false).height;
				var obj_c=getComputedStyle(odiv,false).backgroundColor;
				if(name=='width'){
					 w=parseInt(obj_w)+parseInt(value)*i+'px';
					 i+=1;
					odiv.style[name]=w;
				}
				else if(name=='height'){
					h=parseInt(obj_h)+parseInt(value)*j+'px';
					j+=1;
					odiv.style[name]=h;
				}else if(name=='backgroundColor')
				{
					if(c==value.length)
						c=0;
					odiv.style[name]=value[c]
					c+=1;  
				}

			}
		</script>
	</head>
	<body>
		<input  type="button" value="变宽" onclick="cool('width','100');"/>
		<input  type="button" value="变高" onclick="cool('height','100');"/>
		<input  type="button" value="变红" onclick="cool('backgroundColor',['red','yellow','pink','purple']);"/>
		<br>
		<div id="box"></div>
	</body>
</html>

为了方便用户更好使用web组态,最近提供了用户自定义组件的功能。在实施项目中就可以使用自己的组件了!



首先我们登陆系统就会看到新增的组件管理选项 如下图:


点击添加组件选择2D组件我们就可以建立一个自己的组件了


《组件设计器》由 基础设置(包括名称 code 类型 状态 icon 次序号 )HTML编辑区域 CSS编辑区域 JS编辑区域预览区域组成。

首先我们给组件 起一个‘名字’ 和 ‘code’,在url输入框中可以给组件设置一个icon。点击保存系统会为我们建立一个组件模板。

由于web组态是由vue开发的所以开发组件也需要vue的的基础知识。建议去看下vue的教程及生命周期,以方便开发组件。以下我附上vue生命周期图及组件代码。

我们就开始设计一个炫酷的按钮作为例子

HTML代码如下:

<a href="#" class="btn123" :style="imrstyle" v-show="controlProp.commProp.visible">{{controlProp.textProp.text}}</a>

这里:

style="imrstyle":样式 在web组态设计器中呈现的样式

v-show="controlProp.commProp.visible":可见性 在web组态设计器中可实现显示或闪烁

{{controlProp.textProp.text}}:文本 对应组件的文本属性

更多属性请参考:http://krmes.com:8000/md/design/#%E7%BB%84%E4%BB%B6%E5%9F%BA%E7%A1%80%E5%B1%9E%E6%80%A7

CSS代码如下:

.btn123 {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      box-sizing: border-box;
      z-index: 1;
    }
    
    .btn123:hover {
      animation: animate 8s linear infinite;
    }
    
    @keyframes animate {
      0% {
        background-position: 0%;
      }
      100% {
        background-position: 400%;
      }
    }
    
    .btn123::before {
      content: '';
      position: absolute;
      top: -5px;
      left: -5px;
      right: -5px;
      bottom: -5px;
      z-index: -9999;
      background: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
      background-size: 400%;
      border-radius: 40px;
      opacity: 0;
      transition: 0.5s;
    }
    
    .btn123:hover::before {
      filter: blur(20px);
      opacity: 1;
      animation: animate 8s linear infinite;
    }
    
    

JS代码如下:

export default {
  props: {
    controlProp: Object //作为web组态设计器的继承属性
  },
  data() {
    return {
    }
  },
  computed: {
    imrstyle: function () {  //imrstyle 作为web组态设计器的控制样式
      let maxWidth=this.controlProp.commProp.width
      let maxHeight=this.controlProp.commProp.height
      if (this.controlProp.textProp && this.controlProp.textProp.padding) {
        maxWidth=maxWidth - this.controlProp.textProp.padding * 2
        maxHeight=maxHeight - this.controlProp.textProp.padding * 2
      }
      return {
        // 'max-width': maxWidth+ 'px',
        // 'max-height': maxHeight+ 'px',
        width: '100%',
        height: '100%',
        'box-sizing': 'content-box',
        background: 'transparent',
        'color': this.controlProp.textProp ? this.controlProp.textProp.fontcolor : '',
        'font-size': this.controlProp.textProp ? this.controlProp.textProp.fontsize + 'px' : '',
        'text-align': this.controlProp.textProp ? this.controlProp.textProp.horizonalign : '',
        'line-height': maxHeight + 'px',
        'vertical-align': this.controlProp.textProp ? this.controlProp.textProp.verticalalign : '',
        'font-family': this.controlProp.textProp ? this.controlProp.textProp.fontfamily : '',
        'font-weight': this.controlProp.textProp ? (this.controlProp.textProp.fontweight ? 600 : 500) : '',
        'font-style': this.controlProp.textProp ? (this.controlProp.textProp.fontitalic ? 'italic' : 'normal') : ''
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    initImports() {
      return {

      }
    },
    initProp() {   //初始化状态 (基础属性 特殊属性 文本属性)
      return {
        commProp: { // 基础属性  
          width: 80,
          height: 30,
          borderwidth: 0,
          backgroundColor: 'linear-gradient(90deg, #03a9f4, #00FFFF)',
          borderradius:5
        },
        spProp:{ // 特殊属性
          },
        textProp: { // 文本属性
          text: 'Button',
          fontsize: 12,
          fontcolor: 'black',
          horizonalign: 'center',
          verticalalign: 'middle',
          padding: 0,
          margin: 0
        },
        spPropSetting: [ // 特殊属性 textinput/numberinput/switch/select/colorPicker/codeInput/dateInput/imgSelect/setup
        ]
      }
    }
  }
}

这里需要注意:

initProp():方法中实现对组件的 基础属性 文本属性 特殊属性的初始化配置

更多属性配置参考:http://krmes.com:8000/md/design/#%E7%BB%84%E4%BB%B6%E5%9F%BA%E7%A1%80%E5%B1%9E%E6%80%A7

点击保存这样我们设计的组件就显示出来了!是不是很简单。


这样在我们的web组态中就可以使用我们自定义的组件了![大笑][大笑][大笑]