Есть список вопросов на странице, который может быть очень длинным. Я создал небольшой список здесь в примере кода. Пользователь прокручивает вопрос, нажимает на него, и список скрывается, появляются отдельные вопросы. Пользователь читает и нажимает «X», чтобы закрыть его, и исходный список снова появляется в ТО ЖЕ ПОЛОЖЕНИИ (что я хочу, но не получаю).
Проблема заключается в том, что список появляется сверхувместо сохранения позиции прокрутки.
При переходе к показу / скрытию fadeIN / fadeOut с использованием event.preventDefault (), похоже, ничего не меняет.
Код использует Bootstrap 4 /Jquery.
(Увеличьте браузер, чтобы получить полосы прокрутки для тестирования)
Код ниже:
//********************************************************************************************************
// Clone Question, display on its own, hide existing list of questions
//********************************************************************************************************
$(document).on('click', '[data-name="question"]', function(){
event.preventDefault();
var qid = $(this).attr("data-qid");//id of the question being answered
//var clone = $(this).clone();//clone entire question element to paste into new space
var clone = $('#header'+qid).clone();//clone entire question element to paste into new space
$('#load_data').hide();
$(clone).hide().appendTo('#load_single_answer').fadeIn(1000);
var html = '<div class="container">'+//provide an X button to close the single question display
'<div class="exit btn btn-light btn-space col-12 box displ text-center align-self-center" style="font-size:120%"><strong>X</strong></div>'+
'</div>';
$(html).appendTo('#load_single_answer');
});
//**********************************************************************
// Close single question display, bring back the list of questions
//**********************************************************************
$(document).on('click', '.exit', function(){
event.preventDefault();
//restore all background data
$('#load_data').fadeIn(500);
$('#load_single_answer').empty();//clear out single answer data
});
<!DOCTYPE html>
<html lang="en">
<head>
<!-- JQuery -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous" type="text/javascript" charset="utf-8"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>
<body style="background-color:#f2f2f2;">
<!-- loading point for displaying single questions and answers -->
<div class='container' style="max-width:900px;">
<div id="load_single_answer"></div>
</div>
<div id="main">
<!-- Question and Answers loaded here via ajax call -->
<div id = "dataX" class="container p-0" style="max-width: 900px;">
<div id="load_data">
<!-- BEGINNING LIST OF QUESTIONS-->
<!-- questions here -->
<div data-name="results">
<!-- individual question container with DETAILS link -->
<div class="card-header" id="header165" style="border-radius:10px;background-color:white;border:none;">
<div data-qid="165" data-aid="0" data-name="question" style="cursor: pointer;">
<div style="margin-bottom:-7px;">
<h5 style="font-family: "Verdana", Verdana;font-weight: bolder;margin-top:0px;"><span style="white-space:pre-wrap;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam?</span>
</div>
</div>
</div>
<br>
</div>
<!-- questions here -->
<div data-name="results">
<!-- individual question container with DETAILS link -->
<div class="card-header" id="header166" style="border-radius:10px;background-color:white;border:none;">
<div data-qid="166" data-aid="0" data-name="question" style="cursor: pointer;">
<div style="margin-bottom:-7px;">
<h5 style="font-family: "Verdana", Verdana;font-weight: bolder;margin-top:0px;"><span style="white-space:pre-wrap;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam?</span>
</div>
</div>
</div>
<br>
</div>
<!-- questions here -->
<div data-name="results">
<!-- individual question container with DETAILS link -->
<div class="card-header" id="header167" style="border-radius:10px;background-color:white;border:none;">
<div data-qid="167" data-aid="0" data-name="question" style="cursor: pointer;">
<div style="margin-bottom:-7px;">
<h5 style="font-family: "Verdana", Verdana;font-weight: bolder;margin-top:0px;"><span style="white-space:pre-wrap;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam?</span>
</div>
</div>
</div>
<br>
</div>
<!-- questions here -->
<div data-name="results">
<!-- individual question container with DETAILS link -->
<div class="card-header" id="header168" style="border-radius:10px;background-color:white;border:none;">
<div data-qid="168" data-aid="0" data-name="question" style="cursor: pointer;">
<div style="margin-bottom:-7px;">
<h5 style="font-family: "Verdana", Verdana;font-weight: bolder;margin-top:0px;"><span style="white-space:pre-wrap;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam?</span>
</div>
</div>
</div>
<br>
</div>
<!-- questions here -->
<div data-name="results">
<!-- individual question container with DETAILS link -->
<div class="card-header" id="header169" style="border-radius:10px;background-color:white;border:none;">
<div data-qid="169" data-aid="0" data-name="question" style="cursor: pointer;">
<div style="margin-bottom:-7px;">
<h5 style="font-family: "Verdana", Verdana;font-weight: bolder;margin-top:0px;"><span style="white-space:pre-wrap;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam?</span>
</div>
</div>
</div>
<br>
</div>
<!-- questions here -->
<div data-name="results">
<!-- individual question container with DETAILS link -->
<div class="card-header" id="header170" style="border-radius:10px;background-color:white;border:none;">
<div data-qid="170" data-aid="0" data-name="question" style="cursor: pointer;">
<div style="margin-bottom:-7px;">
<h5 style="font-family: "Verdana", Verdana;font-weight: bolder;margin-top:0px;"><span style="white-space:pre-wrap;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam?</span>
</div>
</div>
</div>
<br>
</div>
<!-- questions here -->
<div data-name="results">
<!-- individual question container with DETAILS link -->
<div class="card-header" id="header171" style="border-radius:10px;background-color:white;border:none;">
<div data-qid="171" data-aid="0" data-name="question" style="cursor: pointer;">
<div style="margin-bottom:-7px;">
<h5 style="font-family: "Verdana", Verdana;font-weight: bolder;margin-top:0px;"><span style="white-space:pre-wrap;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam?</span>
</div>
</div>
</div>
<br>
</div>
<!-- questions here -->
<div data-name="results">
<!-- individual question container with DETAILS link -->
<div class="card-header" id="header172" style="border-radius:10px;background-color:white;border:none;">
<div data-qid="172" data-aid="0" data-name="question" style="cursor: pointer;">
<div style="margin-bottom:-7px;">
<h5 style="font-family: "Verdana", Verdana;font-weight: bolder;margin-top:0px;"><span style="white-space:pre-wrap;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam?</span>
</div>
</div>
</div>
<br>
</div>
<!-- END OF LIST OF QUESTIONS -->
</div>
</div>
</div>
</body>