gpt4 book ai didi

html - 自定义 css 光标过渡

转载 作者:太空宇宙 更新时间:2023-11-04 01:02:19 25 4
gpt4 key购买 nike

我有一个 ul,每个 li 上都有一些元素,我有一个不同的光标,它工作得很好,但不流畅,所以是否有任何方法可以从默认光标过渡到自定义图像?

HTML

<section class="who">
<ul>
<li>Purpose</li>
<li>Creative</li>
<li>Enthusiastic</li>
<li>Adventure</li>
<li>Curious</li>
<li>Passionate</li>
</ul>
</section>

CSS

.who ul li:first-of-type {
cursor: url('/assets/media/icons/target.png'), auto;
}

等等,你有什么想法。

最佳答案

那么您是否希望不快速地更改图像光标,而是像淡入/淡出效果一样平滑?在这种情况下,您还涉及 javascript:

  1. .who ul li { cursor: none; }
  2. 创建 <div class="cursor-container"></div>
  3. .cursor-container { position:absolute; z-index:100; }
  4. 把你所有的图片放到一个
  5. .cursor-container .image-container { position:absolute; }
  6. 使用 javascript 的 onMouseMove( ) 获取光标坐标并将 .cursor-container 移动到旁边 ( see here how )
  7. 使用 CSS 隐藏所有图像 .cursor-container .image-container { display:none }
  8. <li class="whatever" onmouseover="changeCursor()"> 这样组织您的列表每个都有不同的类名
  9. 使用 JS 你必须 switch() 所有 <li>的类并将 CSS3 过渡(如不透明度)应用到正确的类。

希望有用。

下面的代码是一个示例(不适用于 jsfiddle,请复制到您的本地页面)。

<!DOCTYPE html>
<html>
<head>
<title> </title>

<style type="text/css">
.who ul {
font-size: 0px;
cursor: none;
}
.who ul li {
display: inline-block;
padding: 10px;
background-color: #CCC;
color: #FFF;
font-size: 18px;
cursor: none;
}
.who ul li.facebook {
background-color: #3b5998;
}
.who ul li.twitter {
background-color: #1dcaff;
}
.who ul li.youtube {
background-color: #ff0000;
}
.who ul li.whatsapp {
background-color: #075e54;
}
.who ul li.instagram {
background-color: #e95950;
}

#cursor-container {
position: absolute;
width: 20px;
height: 20px;
display: none;
cursor: none;
}
#cursor-container.visible {
display: block;
}
#cursor-container .image-container {
position: absolute;
width: inherit;
height: inherit;
opacity: 0;
background-color: #FFF;

transition: opacity 0.6s linear;
-moz-transition: opacity 0.6s linear;
-o-transition: opacity 0.6s linear;
-webkit-transition: opacity 0.6s linear;
}
#cursor-container .image-container.fadeIn {
opacity: 1;
}
#cursor-container .image-container img {
width: 100%;
height: 100%;
}

</style>
</head>
<body>

<div class="who">
<ul>
<li class="facebook" onmouseover="changeCursor(this);">facebook</li>
<li class="twitter" onmouseover="changeCursor(this);">twitter</li>
<li class="youtube" onmouseover="changeCursor(this);">youtube</li>
<li class="whatsapp" onmouseover="changeCursor(this);">whatsapp</li>
<li class="instagram" onmouseover="changeCursor(this);">instagram</li>
</ul>
</div>
<div id="cursor-container">
<div class="image-container facebook">
<img src="http://icons.iconarchive.com/icons/sicons/flat-shadow-social/256/facebook-icon.png" />
</div>
<div class="image-container twitter">
<img src="https://cdn2.iconfinder.com/data/icons/metro-uinvert-dock/256/Twitter_NEW.png" />
</div>
<div class="image-container youtube">
<img src="https://image.flaticon.com/icons/png/128/185/185983.png" />
</div>
<div class="image-container whatsapp">
<img src="http://aux2.iconspalace.com/uploads/whatsapp-icon-256-829186234.png" />
</div>
<div class="image-container instagram">
<img src="https://image.flaticon.com/icons/png/128/174/174855.png" />
</div>
</div>

<script type="text/javascript">

var cursor = document.getElementById('cursor-container');
var cursorImages = cursor.getElementsByClassName('image-container');

(() => {

let list = document.getElementsByClassName('who')[0].getElementsByTagName('ul')[0];

document.addEventListener('mousemove', (e) => {

cursor.style.top = e.clientY+'px';
cursor.style.left = e.clientX+'px';
});

list.addEventListener('mouseenter', () => {

cursor.classList.add('visible');
});
list.addEventListener('mouseleave', () => {

cursor.classList.remove('visible');
});

})();


function changeCursor(elem) {

Array.from(cursorImages).map((elem) => {

elem.classList.remove('fadeIn');
});

switch(elem.className) {

case 'facebook': {
cursor.getElementsByClassName('facebook')[0].classList.add('fadeIn');
break;
}
case 'twitter': {
cursor.getElementsByClassName('twitter')[0].classList.add('fadeIn');
break;
}
case 'youtube': {
cursor.getElementsByClassName('youtube')[0].classList.add('fadeIn');
break;
}
case 'whatsapp': {
cursor.getElementsByClassName('whatsapp')[0].classList.add('fadeIn');
break;
}
case 'instagram': {
cursor.getElementsByClassName('instagram')[0].classList.add('fadeIn');
break;
}
default:
break;
}
}

</script>

</body>
</html>

关于html - 自定义 css 光标过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com