如何在刷新时保持我在页面上的位置 (AJAX)


How can I mantain my position on the page on refresh (AJAX)

我知道这只有在 AJAX 上才有可能,但我从未使用过 AJAX...在我的网站上,您可以保留游戏万智牌中的卡牌列表,这是相当的列表。您必须按下一个按钮才能添加卡片,每张卡片都有此按钮,当您添加它时,它会将卡片添加到您的列表中,然后刷新并再次出现在页面顶部。我怎样才能让它保持它的位置?

在红色方块内,您可以按添加或删除:http://prntscr.com/5uq6ak

功能.php(我只展示2个功能,删除和添加卡片)

//Add card to collection
function addCardToCollection($conn, $userID, $cardID){
//Checks if the cards is already added for this user
    $queryGetCard = 'SELECT user_id, card_id FROM collection WHERE user_id = '.$userID.' AND card_id = '.$cardID;
    $checkCollection = $conn->query($queryGetCard);
    if($checkCollection->fetch_assoc() > 0){return 'Deze kaart hebt u al.';}
//Adds card to the database
    $queryAddCard = 'INSERT INTO collection (user_id, card_id) VALUES ('.$userID.','.$cardID.')';
    if($conn->query($queryAddCard)){return 'Kaart toegevoegd.';}
    else{return 'Kaart niet toegevoegd.';}
}
//Remove card from collection
function removeCardFromCollection($conn, $userID, $cardID){
//Checks if the cards is in the collection
    $queryGetCard = 'SELECT user_id, card_id FROM collection WHERE user_id = '.$userID.' AND card_id = '.$cardID;
    $checkCollection = $conn->query($queryGetCard);
    if($checkCollection->fetch_assoc() == 0){return 'Deze kaart hebt u nog niet.';}
//Remove card from the database
    $queryAddCard = 'DELETE FROM collection WHERE user_id = '.$userID.' AND card_id = '.$cardID;
    if($conn->query($queryAddCard)){return 'Kaart verwijderd uit uw collectie.';}
    else{return 'Kaart niet verwijderd uit uw collectie.';}
}

设置.php(用于添加和删除的按钮)

if(login_check($mysqli) == true) {
    $cardsHTML.='<br><b>Deze kaart heb ik...
    <a href="' . $baseURL . 'set.php?id=' . $_GET['id'] . '&cardID=' . $value['id'] . '&collection=add">
        <div class="glyphicon glyphicon-ok green"></div>
    </a> |
    <a href="' . $baseURL . 'set.php?id=' . $_GET['id'] . '&cardID=' . $value['id'] . '&collection=remove">
        <div class="glyphicon glyphicon-remove red"></div>
    </a>
    </b>';
}

在插入过程中添加带有时间戳的列。并获取时间戳的数据升序。显示 card.it 不会改变。

你有没有想过使用JS来做到这一点?

注意:这需要JQuery插件,你可以从中获取许多CDN,谷歌是最可靠的之一

这里
$(document).ready(function(){
    var storePosition = {     /// initializing the storeposition
        topCoordinate : null
    }

     storePosition.topCoordinate =  $(this).offset().top;   /// storing the position

    if(storePosition.topCoordinate !== 0){   // if we are at the top no point doing anything
     $("html, body").animate({"scrollTop":  storePosition.topCoordinate}, 500);

        }
});

由本问题提供

也只是一个注释...请停止使用非参数化查询。考虑使用 MySQLi,因为它与 MySql 语法相比没有太大的提升,并且会给你一些面向对象编程的良好实践。