最近工作需要增强用户体验,项目经理说下拉框架用户体验太差,于是乎我开始想了如下指令。这个指令可以让用户点击的时候把下拉或者其他的响应显示的东西不会因为屏幕的滚动而看不见,也就是让用户看见他想看见的。
具体如下:
define(['angular'], function(){
var commonDirectives = angular.module("commonDirectives", []);
commonDirectives.directive("toLocate", function () {
return {
restrict: "AE",
scope:{
tid:"@",
pscope:"@"
},
link: function (scope, element, attrs) {
element.bind("click", function (event) {
event.stopPropagation();
var scroll_offset = $("#"+scope.tid).offset();
if(scope.pscope == undefined){
$("body,html").animate({
scrollTop: scroll_offset.top
}, 1000);
}else{
$("."+scope.pscope).animate({
scrollTop: scroll_offset.top
}, 1000);
}
})
}
}
})
})
2020-10-12 更新,获得元素在屏幕中的位置 : el.getBoundingClientRect();