[英]Scroll - On click not able to scroll the custom scroll inside fixed sidebar
我做了一個固定的側邊欄並應用了自定義滾動條(iemcustomscrollbar),但是我想在單擊anchor標簽時(在手風琴內部)我想在側邊欄頂部滾動手風琴,為此我使用了相同的工具當側邊欄不粘時將正文或html滾動到單擊的元素頂部位置時使用的代碼。
我做錯了什么?
不粘
如果側邊欄不粘滯,則為以下代碼
$('.card > .collapse').on('shown.bs.collapse', function(e){
e.preventDefault();
e.stopPropagation();
console.log('click');
var scrollToCategory = $(this).parent().offset().top;
var body = $("html, body");
body.stop().animate({scrollTop:scrollToCategory}, 500, 'swing', function() {
// alert("Finished animating");
});
})
黏
如果側邊欄是粘性的,這是代碼
$('.card > .collapse').on('shown.bs.collapse', function(e){
e.preventDefault();
e.stopPropagation();
console.log('click');
var scrollToCategory = $(this).parent().offset().top;
var body = $("#sidebar-wrapper");
body.stop().animate({scrollTop:scrollToCategory}, 500, 'swing', function() {
// alert("Finished animating");
});
})
這是jsfiddle鏈接
注意: Sticky示例在jsfiddle中似乎不起作用,請將該示例復制並粘貼到另一個自定義文件中:)
最后,
經過大量搜索,我做到了。 它只是m.custom滾動條中已經存在的一段代碼
解決方法如下:
$('.card > .collapse').on('shown.bs.collapse', function(e){
e.preventDefault();
e.stopPropagation();
var childPos = $(this).parent().offset();
var parentPos = $('#sidebar').offset();
var childOffset = {
top: childPos.top - parentPos.top,
left: childPos.left - parentPos.left
}
$("#sidebar-wrapper").mCustomScrollbar('scrollTo', '-='+childOffset.top);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.