我有三个标签,我想在它们之间转移时保持位置滚动
示例:当他移动到第二个选项卡并将其传递到页面末尾并回到我希望她成为第一个选项卡的中间时(例如离开之前),他位于页面中间的第一个选项卡
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>My App</title>
<link rel="stylesheet" href="../dist/css/framework7.ios.min.css">
<link rel="stylesheet" href="../dist/css/framework7.ios.colors.min.css">
</head>
<body>
<div class="views">
<div class="view view-main">
<div class="pages navbar-fixed">
<div data-page="home" class="page with-subnavbar">
<div class="navbar">
<div class="navbar-inner">
<div class="center">Swipeable Tabs</div>
<div class="subnavbar">
<div class="buttons-row"><a href="#tab1" class="button active tab-link">Tab 1</a><a href="#tab2" class="button tab-link">Tab 2</a><a href="#tab3" class="button tab-link">Tab 3</a></div>
</div>
</div>
</div>
<div class="tabs-swipeable-wrap">
<div class="tabs">
<div id="tab1" class="page-content with-subnavbar tab active">
<div class="content-block">
<p>This is tab 1 content</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum mi quis felis scelerisque faucibus. Aliquam ut commodo justo. Mauris vitae pharetra arcu. Sed tincidunt dui et nibh auctor pretium. Nam accumsan fermentum sem. Suspendisse potenti. Nulla sed orci malesuada, pellentesque elit vitae, cursus lorem. Praesent et vehicula sapien, ut rhoncus quam. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vitae mi nec lorem aliquet venenatis quis nec nibh. Aenean sit amet leo ligula. Fusce in massa et nisl dictum ultricies et vitae dui. Sed sagittis quis diam sed lobortis. Donec in massa pharetra, tristique purus vitae, consequat mauris. Aliquam tellus ante, pharetra in mattis ut, dictum quis erat.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum mi quis felis scelerisque faucibus. Aliquam ut commodo justo. Mauris vitae pharetra arcu. Sed tincidunt dui et nibh auctor pretium. Nam accumsan fermentum sem. Suspendisse potenti. Nulla sed orci malesuada, pellentesque elit vitae, cursus lorem. Praesent et vehicula sapien, ut rhoncus quam. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vitae mi nec lorem aliquet venenatis quis nec nibh. Aenean sit amet leo ligula. Fusce in massa et nisl dictum ultricies et vitae dui. Sed sagittis quis diam sed lobortis. Donec in massa pharetra, tristique purus vitae, consequat mauris. Aliquam tellus ante, pharetra in mattis ut, dictum quis erat.</p>
</div>
</div>
<div id="tab2" class="page-content tab">
<div class="content-block">
<p>This is tab 2 content</p>
<p>Ut ac lobortis lacus, non pellentesque arcu. Quisque sodales sapien malesuada, condimentum nunc at, viverra lacus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vivamus eu pulvinar turpis, id tristique quam. Aenean venenatis molestie diam, sit amet condimentum nisl pretium id. Donec diam tortor, mollis in vehicula id, vehicula consectetur nulla. Quisque posuere rutrum mauris, eu rutrum turpis blandit at. Proin volutpat tortor sit amet metus porttitor accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Ut dapibus posuere dictum.</p>
<p>Ut ac lobortis lacus, non pellentesque arcu. Quisque sodales sapien malesuada, condimentum nunc at, viverra lacus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vivamus eu pulvinar turpis, id tristique quam. Aenean venenatis molestie diam, sit amet condimentum nisl pretium id. Donec diam tortor, mollis in vehicula id, vehicula consectetur nulla. Quisque posuere rutrum mauris, eu rutrum turpis blandit at. Proin volutpat tortor sit amet metus porttitor accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Ut dapibus posuere dictum.</p>
</div>
</div>
<div id="tab3" class="page-content tab">
<div class="content-block">
<p>This is tab 3 content</p>
<p>Nulla gravida libero eget lobortis iaculis. In sed elit eu nibh adipiscing faucibus. Sed ac accumsan lacus. In ut diam quis turpis fringilla volutpat. In ultrices dignissim consequat. Cras pretium tortor et lorem condimentum posuere. Nulla facilisi. Suspendisse pretium egestas lacus ac laoreet. Mauris rhoncus quis ipsum quis tristique. Vivamus ultricies urna quis nunc egestas, in euismod turpis fringilla. Nam tellus massa, vehicula eu sapien non, dapibus tempor lorem. Fusce placerat orci arcu, eu dignissim enim porttitor vel. Nullam porttitor vel dolor sed feugiat. Suspendisse potenti. Maecenas ac mattis odio. Sed vel ultricies lacus, sed posuere libero.</p>
<p>Nulla gravida libero eget lobortis iaculis. In sed elit eu nibh adipiscing faucibus. Sed ac accumsan lacus. In ut diam quis turpis fringilla volutpat. In ultrices dignissim consequat. Cras pretium tortor et lorem condimentum posuere. Nulla facilisi. Suspendisse pretium egestas lacus ac laoreet. Mauris rhoncus quis ipsum quis tristique. Vivamus ultricies urna quis nunc egestas, in euismod turpis fringilla. Nam tellus massa, vehicula eu sapien non, dapibus tempor lorem. Fusce placerat orci arcu, eu dignissim enim porttitor vel. Nullam porttitor vel dolor sed feugiat. Suspendisse potenti. Maecenas ac mattis odio. Sed vel ultricies lacus, sed posuere libero.</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="../dist/js/framework7.min.js"></script>
<script>var myApp = new Framework7(); </script>
</body>
</html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>collapsible demo</title>
<link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script type='text/javascript'>//<![CDATA[
$(function(){
$("a[data-role=tab]").each(function () {
var anchor = $(this);
anchor.bind("click", function () {
$.mobile.changePage(anchor.attr("href"), {
transition: "none",
changeHash: false
});
return false;
});
});
$("div[data-role=page]").bind("pagebeforeshow", function (e, data) {
$.mobile.silentScroll(0);
$.mobile.changePage.defaults.transition = 'slide';
});
});//]]>
</script>
</head>
<body>
<div id="page-1" data-role="page">
<div data-role="header">
<a href="#" data-icon="arrow-l" data-iconpos="left" data-rel="back" data-transition="slide" data-direction="reverse">Back</a>
<h1>Page 1</h1>
</div>
<div data-role="content">
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
<p>Page 1 content</p>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="#page-1" data-role="tab" data-icon="grid" class="ui-btn-active">Page 1</a></li>
<li><a href="#page-2" data-role="tab" data-icon="grid">Page 2</a></li>
<li><a href="#page-3" data-role="tab" data-icon="grid">Page 3</a></li>
</ul>
</div>
</div>
</div>
<div id="page-2" data-role="page">
<div data-role="header">
<a href="#" data-icon="arrow-l" data-iconpos="left" data-rel="back" data-transition="slide" data-direction="reverse">Back</a>
<h1>Page 2</h1>
</div>
<div data-role="content">
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
<p>Page 2 content</p>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="#page-1" data-role="tab" data-icon="grid">Page 1</a></li>
<li><a href="#page-2" data-role="tab" data-icon="grid" class="ui-btn-active">Page 2</a></li>
<li><a href="#page-3" data-role="tab" data-icon="grid">Page 3</a></li>
</ul>
</div>
</div>
</div>
<div id="page-3" data-role="page">
<div data-role="header">
<a href="#" data-icon="arrow-l" data-iconpos="left" data-rel="back" data-transition="slide" data-direction="reverse">Back</a>
<h1>Page 3</h1>
</div>
<div data-role="content">
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
<p>Page 3 content</p>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="#page-1" data-role="tab" data-icon="grid">Page 1</a></li>
<li><a href="#page-2" data-role="tab" data-icon="grid">Page 2</a></li>
<li><a href="#page-3" data-role="tab" data-icon="grid" class="ui-btn-active">Page 3</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
最佳答案
$("[data-role='tabs'] [data-role='navbar'] a").on("click", function() {
const $domPre = $("[data-role='tabs'] [data-role='navbar'] a.ui-btn-active");
const $dom = $(this);
const hrefPre = $domPre.attr("href");
const href = $dom.attr("href");
const top = $dom.data("top")
if (hrefPre.match(/^#/)) {
$domPre.data("top", $(window).scrollTop());
}
if (href.match(/^#/) && typeof top === "number") {
setTimeout(function() {
$(window).scrollTop(top)
});
} else {
setTimeout(function() {
$(window).scrollTop(0)
});
}
});
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>collapsible demo</title>
<link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="header" data-position="fixed">
<h1>jQuery Mobile Example</h1>
</div>
<div role="main" class="ui-content">
<div data-role="tabs">
<div data-role="navbar" style="position: fixed; width: 100%;">
<ul>
<li><a href="#fragment-1" class="ui-btn-active">One</a></li>
<li><a href="#fragment-2">Two</a></li>
<li><a href="#fragment-3">Three</a></li>
</ul>
</div><br>
<div id="fragment-1">
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
<p>page 1</p>
</div>
<div id="fragment-2">
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
<p>page 2</p>
</div>
<div id="fragment-3">
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
<p>page 3</p>
</div>
</div>
</div>
</div>
</body>
</html>
关于javascript - 使用JavaScript或jquery保留每个选项卡的位置滚动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42781282/