下拉菜单向右展开而不是向下展开可以通过以下几种方式实现:
.dropdown {
float: left;
position: relative;
}
.dropdown-content {
display: none;
position: absolute;
right: 0;
top: 100%;
}
.dropdown:hover .dropdown-content {
display: block;
}
var dropdown = document.querySelector('.dropdown');
var dropdownContent = document.querySelector('.dropdown-content');
dropdown.addEventListener('mouseover', function() {
var rect = dropdown.getBoundingClientRect();
dropdownContent.style.left = rect.width + 'px';
dropdownContent.style.display = 'block';
});
dropdown.addEventListener('mouseout', function() {
dropdownContent.style.display = 'none';
});
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.dropdown {
float: left;
position: relative;
}
.dropdown-content {
display: none;
position: absolute;
right: 0;
top: 100%;
}
</style>
<script>
$(document).ready(function() {
$('.dropdown').hover(function() {
$(this).find('.dropdown-content').stop().animate({left: '100%'}, 200);
}, function() {
$(this).find('.dropdown-content').stop().animate({left: '0'}, 200);
});
});
</script>
</head>
<body>
<div class="dropdown">
<button>下拉菜单</button>
<div class="dropdown-content">
<a href="#">菜单项1</a>
<a href="#">菜单项2</a>
<a href="#">菜单项3</a>
</div>
</div>
</body>
</html>
以上是三种常见的实现下拉菜单向右展开的方法,可以根据具体需求选择适合的方式进行实现。
领取专属 10元无门槛券
手把手带您无忧上云