RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 808919
Accepted
LiaVa
LiaVa
Asked:2020-04-03 21:54:32 +0000 UTC2020-04-03 21:54:32 +0000 UTC 2020-04-03 21:54:32 +0000 UTC

突出显示光标的最佳方法是什么?

  • 772

有必要使按钮上的光标突出显示。我的意思是,这样当我在按钮上滑动它时,光标下就会出现某种渐变、背景、圆圈......不是很重要的东西。

问题是:实现这一点的最佳方法是什么?在按钮本身的背景上移动一些东西,或者只是创建一个带有背景的透明块并已经移动它?

javascript
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Best Answer
    Kirill
    2020-04-03T23:13:08Z2020-04-03T23:13:08Z

    我们不是在寻找简单的方法!

    var curBg = document.querySelectorAll('.cur-bg'),
      mouseBg = document.getElementById('mouse-bg');
    
    for (var i = 0; i < curBg.length; i++) {
      curBg[i].addEventListener('mousemove', foo, false);
      curBg[i].addEventListener('mouseleave', boo, false);
    }
    
    function foo(event) {
      mouseBg.style.display = 'block';
      mouseBg.style.left = (event.clientX - 7) + 'px'
      mouseBg.style.top = (event.clientY - 7) + 'px'
    }
    
    function boo() {
      mouseBg.style.display = 'none';
    }
      #mouse-bg {
      width: 15px;
      height: 15px;
      border-radius: 8px;
      background: linear-gradient(270deg, #41bd9d, #d72222);
      background-size: 400% 400%;
      display: none;
      position: absolute;
      pointer-events: none;
      -webkit-animation: AnimationName 1s ease infinite;
      -o-animation: AnimationName 1s ease infinite;
      animation: AnimationName 1s ease infinite;
    }
    
    @-webkit-keyframes AnimationName {
      0% {background-position: 0% 50%}
      50% {background-position: 100% 50%}
      100% {background-position: 0% 50%}
    }
    
    @-o-keyframes AnimationName {
      0% {background-position: 0% 50%}
      50% {background-position: 100% 50%}
      100% {background-position: 0% 50%}
    }
    
    @keyframes AnimationName {
      0% {background-position: 0% 50%}
      50% {background-position: 100% 50%}
    <input class="cur-bg" type="button" value="button" />
    <button class="cur-bg">button</button>
    <span class="cur-bg">button</span>
    
    <div id="mouse-bg"></div>

    • 10
  2. humster_spb
    2020-04-03T22:52:53Z2020-04-03T22:52:53Z

    最简单的就是画个png-shku,用鼠标悬停在按钮上时的样子替换 在此处输入图像描述

    button {
     padding: 5px;
    }
    
    button:hover {
      cursor: url("https://isstatic.askoverflow.dev/QRS91.png"), default;
    }
    <button>
    КНОПКА
    </button>

    • 9
  3. LiaVa
    2020-04-04T20:11:25Z2020-04-04T20:11:25Z

    感谢大家的回复!

    以基里尔·科鲁什金为榜样,他做了以下事情:

    (不使用类)

    (function () {
    	"use strict";
    	var EX = function (f1, f2) {
    		/**упрощение работы с get и set.
    		возвращает {get:function(){return f1;},set:f2},
    		если f2=true, то set:f1, если f2 нет, то set:undefined
    		 **/
    		return {
    			get: function () {
    				return f1;
    			},
    			set: f2 === true ? f1 : f2 || undefined
    		};
    	};
    	EX.auto = function (f1, f2) {
    		/**тоже упрощает работу с get и set.
    		возвращает:
    		если f2=setget, EX(f1,true)
    		если f2=set, {set:f1}
    		если f2=get, {get:function(){return f1;}}
    		если f2=что-то-другое, {get:f1}
    		 **/
    
    		switch (f2) {
    		case "setget":
    			return EX(f1, !!1);
    		case "set":
    			return {
    				set: fl
    			};
    		case "get":
    			return {
    				get: function () {
    					return f1;
    				}
    			};
    		default:
    			return {
    				get: f1
    			}
    		}
    	};
    	Object.defineProperties(Object.prototype, {
    		Ainit: {
    			/**конструктор объектов.
    			принимает объект и помещает его свойства в тот, на котором вызван Init.
    			если объект содержит конструкции типа l:{set:...,get:...}(только как прямые потомки), то они будут использованы как get l или set l (isEnumerable,isConfigurable,isWritable).
    			если свойство __isParam__ установлено в false l:{__isParam__:false}, то все свойства будут приниматься НЕ как параметры свойствa l (enumerable,configurable,writable,get,set)
    			также get и set можно создать, используя имя самой функции l:function get(){}-
    			get - создаст get с обёрткой
    			l:{
    			get:function (){
    			return get(){};
    			}
    			} - функция не будет выполняться сама
    			set - создаст set
    			setget - создаст set и get для одной функции
    			auto- создаст get без обёртки функции(она выполнится сама при вызове get)
    			 **/
    			enumerable: false,
    			configurable: false,
    			writable: true,
    			value: function (vl) {
    				var tmp = {};
    				for (var nm in vl) {
    					var s = vl[nm] || {},
    					isp = s.__isParam__,
    					sn = s.name || "";
    					delete s.__isParam__;
    					tmp[nm] = (s.set === undefined && s.get === undefined && s.enumerable === undefined && s.configurable === undefined && s.writable === undefined && ["get", "auto", "setget", "set"].indexOf(sn) === -1 && isp !== true) || isp === false ? {
    						enumerable: false,
    						configurable: true,
    						writable: true,
    						value: vl[nm]
    					}
    					 : (s.constructor !== Function ? s : EX.auto(s, sn));
    				}
    				Object.defineProperties(this, tmp);
    			}
    		}
    	});
    	var cp, //cursor position
    	whp = {}, //width, height, position, css position, this
    	dsp,
    	gtc, //styles
    	rect, 
    	win,
    	sensor = !!("ontouchmove" in document), //проверка на сенсор
    	newElem = function (attrs, statics) { //используется вместо class для создания элементов
    		function felement() {
    			return Reflect.construct(HTMLElement, [], felement);
    		}
    		Object.setPrototypeOf(felement.prototype, HTMLElement.prototype);
    		Object.setPrototypeOf(felement, HTMLElement);
    
    		felement.prototype.Ainit(attrs);
    		felement.Ainit(statics);
    
    		return felement;
    	},
    	offset = {},
    	getProps = function (th) {
        offset = th.getBoundingClientRect();
    		gtc = getComputedStyle(th);
    		return {
    			w: parseFloat(gtc.width),
    			h: parseFloat(gtc.height),
    			p: {top:offset.top,left:offset.left},
    			f: gtc.position === "fixed",
    			e: th
    		};
    	};
    	window.elems = []; //все элементы подсветки
    	!sensor && document.addEventListener("mousemove", function (e) {
    		cp = {
    			t: event.clientY,
    			l: event.clientX
    		};
    		elems.forEach(function (th) {
    			whp = getProps(th[0]);
    			whp.b = getProps(th[1]);
    			dsp = {
    				l: whp.e.getAttribute("b-left-despos"),
    				t: whp.e.getAttribute("b-top-despos")
    			};
    			dsp.t ? (
    				dsp.t = Function("it,cursor", "return " + dsp.t)(whp, cp)) : (
    				dsp.t = cp.t - whp.b.h / 2 - whp.p.top //выравнивание центра элемента
    			);
    
    			dsp.l ? (
    				dsp.l = Function("it,cursor", "return " + dsp.l)(whp, cp)) : (
    				dsp.l = cp.l - whp.b.w / 2 - whp.p.left);
    
    			whp.b.e.style.left = dsp.l + "px";
    			whp.b.e.style.top = dsp.t + "px";
    		});
    	});
    	customElements.define("f-dplane", newElem({
    			connectedCallback: function () {
    				if (sensor) {
    					this.setAttribute("sensor", "");
    					return;
    				}
    				var back = document.createElement("div");
    				back.setAttribute("b", "");
    				back.style.top = "200%";
    				back.style.left = "200%";
    				this.appendChild(back);
    				elems.push([this, back]);
    			},
    			disconnectedCallback: function () {
    				var elem,
    				th = this;
    				elems.forEach(function (el, ind) {
    					if (el[0] === th) {
    						elem = ind;
    					}
    				});
    				elems.splice(elem, 1);
    			}
    		}));
    })();
    /*f-dplane default*/
    f-dplane:not([sensor]){
    	cursor:default;
    	display:flex;
    	position:absolute;
    	width:100%;
    	height:100%;
    	overflow:hidden;
    }
    f-dplane:not([sensor]) div{
    	position:absolute;
    	background:radial-gradient(rgba(255, 255, 255, 0.13),transparent,transparent) no-repeat;
    	height:100px;
    	width:100px;
    }
    
    /*other*/
    .gg{
      width:400px;
      height:50px;
      outline:none;
      border:none;
      background:black;
      position:relative;
      padding:0;
      color:white;
    }
    .gg > f-dplane {
      top:0;
    }
    .gg > f-dplane > [b] {
      width:400px;
      height:200px;
    }
    <button class="gg">
      <f-dplane></f-dplane>
      asdasdasd
    </button>
    <br>
    <br>
    <button class="gg">
      <f-dplane></f-dplane>
      asdasdasd
    </button>
    <br>
    <br>
    <button class="gg">
      <f-dplane></f-dplane>
      asdasdasd
    </button>
    <br>
    <br>
    <button class="gg">
      <f-dplane></f-dplane>
      asdasdasd
    </button>

    如果使用一个类,js代码量几乎可以减少2倍:

    (function () {
    	"use strict";
    	var cp, //cursor position
    	whp = {}, //width, height, position, css position, this
    	dsp,
    	gtc, //styles
    	rect,
    	win,
    	sensor = !!("ontouchmove" in document), //проверка на сенсор
    	offset = {},
    	getProps = function (th) {
        offset = th.getBoundingClientRect();
    		gtc = getComputedStyle(th);
    		return {
    			w: parseFloat(gtc.width),
    			h: parseFloat(gtc.height),
    			p: {top:offset.top,left:offset.left},
    			f: gtc.position === "fixed",
    			e: th
    		};
    	};
    	window.elems = []; //все элементы подсветки
    	!sensor && document.addEventListener("mousemove", function (e) {
    		cp = {
    			t: event.clientY,
    			l: event.clientX
    		};
    		elems.forEach(function (th) {
    			whp = getProps(th[0]);
    			whp.b = getProps(th[1]);
    			dsp = {
    				l: whp.e.getAttribute("b-left-despos"),
    				t: whp.e.getAttribute("b-top-despos")
    			};
    			dsp.t ? (
    				dsp.t = Function("it,cursor", "return " + dsp.t)(whp, cp)) : (
    				dsp.t = cp.t - whp.b.h / 2 - whp.p.top //выравнивание центра элемента
    			);
    
    			dsp.l ? (
    				dsp.l = Function("it,cursor", "return " + dsp.l)(whp, cp)) : (
    				dsp.l = cp.l - whp.b.w / 2 - whp.p.left);
    
    			whp.b.e.style.left = dsp.l + "px";
    			whp.b.e.style.top = dsp.t + "px";
    		});
    	});
    	class fdplane extends HTMLElement{
    		connectedCallback() {
    			if (sensor) {
    				this.setAttribute("sensor", "");
    				return;
    			}
    			var back = document.createElement("div");
    			back.setAttribute("b", "");
    			back.style.top = "200%";
    			back.style.left = "200%";
    			this.appendChild(back);
    			elems.push([this, back]);
    		}
    		disconnectedCallback() {
    			var elem,
    			th = this;
    			elems.forEach(function (el, ind) {
    				if (el[0] === th) {
    					elem = ind;
    				}
    			});
    			elems.splice(elem, 1);
    		}
    	}
    	customElements.define("f-dplane", fdplane);
    })();
    /*f-dplane default*/
    f-dplane:not([sensor]){
    	cursor:default;
    	display:flex;
    	position:absolute;
    	width:100%;
    	height:100%;
    	overflow:hidden;
    }
    f-dplane:not([sensor]) div{
    	position:absolute;
    	background:radial-gradient(rgba(255, 255, 255, 0.13),transparent,transparent) no-repeat;
    	height:100px;
    	width:100px;
    }
    
    /*other*/
    .gg{
      width:400px;
      height:50px;
      outline:none;
      border:none;
      background:black;
      position:relative;
      padding:0;
      color:white;
    }
    .gg > f-dplane {
      top:0;
    }
    .gg > f-dplane > [b] {
      width:400px;
      height:200px;
    }
    <button class="gg">
      <f-dplane></f-dplane>
      asdasdasd
    </button>
    <br>
    <br>
    <button class="gg">
      <f-dplane></f-dplane>
      asdasdasd
    </button>
    <br>
    <br>
    <button class="gg">
      <f-dplane></f-dplane>
      asdasdasd
    </button>
    <br>
    <br>
    <button class="gg">
      <f-dplane></f-dplane>
      asdasdasd
    </button>

    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5