我使用此代码在滚动页面时移动项目
$(document).scroll(function() {
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
$("#profile").offset({top:scrollTop+34});
});
此代码显示并隐藏它。
$(document).ready(function() {
$(".various[type=profile]").click(function() {
if($("#profile").attr("clicked") == "yes") {
$("#profile").stop().animate({opacity: 0}, 1000);
setTimeout(function(){$("#profile").css("visibility", "hidden")}, 1000);
$("#profile").attr("clicked", "");
}
else {
$("#profile").css("visibility", "visible");
$("#profile").stop().animate({opacity: 1}, 1000);
$("#profile").attr("clicked", "yes");
}
});
});
这是css
#profile {
position: absolute;
top: 34px;
right: 0;
width: 200px;
visibility: hidden;
z-index: 1000;
opacity: 0;
}
问题是每次点击都会使项目返回初始位置(顶部:34px,右:0px)。使用fadeIn / fadeOut我有同样的问题。
答案 0 :(得分:0)
答案 1 :(得分:0)
你需要做一些事情。
1:您可以使用CSS属性position:fixed
将其固定在直立的角落,而不是使用jQuery定位元素。
#profile {
position: fixed;
top: 34px;
right: 0;
width: 200px;
z-index: 1000;
}
2:您的jQuery代码存在一些问题,无法显示和隐藏。首先,clicked
不是有效的HTML属性。您应该考虑使用$(element).data('clicked')
代替$(element).attr('clicked')
来存储其可见性。接下来,当您设置visibility:hidden
时,click
事件不再在其上注册,因此点击它不会再显示它。
也许this是您正在寻找的效果?