使悬停保持悬停状态,直到下一个链接
Make an hover stay hovered until next link
首先只想说我不是程序员,但我正在尝试复制代码,因为我的javascript不是最好的,所以我失败了。
我尝试复制的css/java/wtv中的"动画"来自本网站上的前3个框链接:http://artcom.de
如您所见,当您将鼠标悬停在框/链接上时,它会一直悬停,直到鼠标越过另一个链接。
我发现了一些使用 javascript 的解决方案,这些解决方案涉及动画和使用 .stop().animate(
函数,但这使得无法在其上编写 css,其中一件事是在悬停不同的链接时放置不同的背景图像(我可以让我只是不能把框悬停直到下一个链接)。
如果您发现我的问题有很多要求,请随时不回答。这是我的第一篇文章,由于编码是我开始的事情,我对问这个问题感到有点紧张。
提前感谢您的任何回复。
您可以使用下面的方法在将有效元素的鼠标悬停上简单地应用"悬停"类,然后将其从类似的限定元素中剥离出来。可以使用transition
在CSS中处理动画
$('div').on('mouseover', function() {
$('div').removeClass('hover');
$(this).addClass('hover');
});
body {
background: black;
}
div {
transition: all 200ms ease-in;
color: #fff;
border: 3px solid #fff;
width: 20%;
display: inline-block;
margin: 0 20px;
text-align: center;
padding: 10px;
box-sizing: border-box;
}
.hover {
color: #000;
background: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>link</div>
<div>link</div>
<div>link</div>
我的解决方案与SW4的解决方案相似,但提供了略有不同的变体。基本上,当您将鼠标悬停在某个项目上时,您希望向其添加一个样式类,同时从所有其他(可能以前悬停的)项目中删除该类。
小提琴 - http://jsfiddle.net/44c9x5eb/2/
在我的小提琴中,我使用一个名为#body
的div
项目,而不是设置实际身体元素的样式。
.HTML
<div id='body'></div>
<div class="item" data-bg="http://www.evokia.com/images/large-background.jpg">1</div>
<div class="item" data-bg="http://p1.pichost.me/i/10/1333648.jpg">2</div>
<div class="item" data-bg="http://www.desktopaper.com/wp-content/uploads/wonderful-fish-wallpaper-large-background.jpg">3</div>
<div class="item" data-bg="http://p1.pichost.me/640/17/1397737.jpg">4</div>
.CSS
#body{
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
background-color:#eee;
z-index:0;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
transition: all 350ms ease-in;
}
.item{
float:left;
text-align:center;
padding:20px;
margin:0 8px;
background:#f00;
position:relative;
z-index:100;
}
.item.selected{
background:#0f0;
}
jQuery
$('.item').mouseover(function(){
//If this item is already selected, forget about it.
if ($(this).hasClass('selected')) return;
//Find the currently selected item, and remove the style class
$('.selected').removeClass('selected');
//Add the style class to this item
$(this).addClass('selected');
//set the body to a different image
$('#body').css( 'background-image', 'url(' + $(this).data('bg') + ')' );
});
基本上你需要做两件事。
- 在 css 中为所选按钮创建一个活动类,该类定义背景颜色
- 绑定到悬停事件,将类添加到将鼠标悬停在按钮上并从其他按钮中删除类。
这将看起来像这样:
$('button').hover(
//this function is called on hoverin
function() {
//remove the css class from all buttons
$('.active').removeClass('active');
//add the class on the hovered over button
$(this).addClass('active');
},
//this function is called on hoverin not needed in this case
function() {}
);
.active {
background-color: white;
}
button {
border: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>button1</button>
<button>button1</button>
<button>button1</button>
请记住,在当今的环境中,许多移动设备被用来访问您的网站。因此,不应依赖悬停事件构建实际功能。即:当您将鼠标悬停在其上时打开的菜单。
- 滚动到容器中的下一个元素-几乎到了
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- 使用ajax将数据从一个步骤发送到下一个步骤的3步表单
- AngularJS&JSON-从Previous&下一个对象
- 使用向下箭头键(与tab键一样)聚焦下一个输入
- 来自文档或下一个静态父级的事件委派
- angularjs移除焦点上的活动类并添加到下一个项目
- 禁用旋转木马中的下一个按钮和上一个按钮
- 当按下一个键时,请多次按下不同的键
- 如何在cycle2.js中悬停img时同时显示上一个/下一个按钮
- javascript onmouseover/omouseout停留在前一个内容中,直到悬停/触发下一个内容
- 使悬停保持悬停状态,直到下一个链接
- 悬停显示下一个“ul”的“李”
- 如何检测鼠标的下一个悬停
- 将鼠标悬停在元素上动画化下一个元素-如何删除内联JS
- 旋转木马与图像的下一个/上一个悬停
- 在悬停时切换下一个元素
- 如何影响图像悬停上一个和下一个图像
- 停止下一个鼠标悬停事件的触发,直到内部函数完成