如何让按钮点击后转到页面的某个部分?

我希望在单击按钮并且该项目已添加到购物篮后,它会自动转到页面的购物篮部分,但我不太知道如何在不弄乱其余 JavaScript 的情况下对此进行编码按钮的代码已经存在。


任何代码片段将不胜感激。


<!DOCTYPE html>

<html>

<head>

   

</head>

<body>


<header class="main-header">

    <nav class="main-nav nav">

    </nav>

</header>


    <div class="shop-items">

        <div class="shop-item">

            <span class="shop-item-title">Album 0</span>&nbsp;

            <div class="shop-item-details">

<span class="shop-item-price">£12.50</span>

            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>

        </div>

  

<div class="shop-item">

        <span class="shop-item-title">Album 2</span>&nbsp;

        <div class="shop-item-details">

            <span class="shop-item-price">£14.50</span>

           

            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>

        </div>

  

  

    <div class="shop-item">

        <span class="shop-item-title">Album 4</span>&nbsp;

        <div class="shop-item-details">

<span class="shop-item-price">£19.50</span>

        <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>

    </div>

    

    </div>

</section>


    <div class="shop-items">

        <div class="shop-item">

            <span class="shop-item-title">Album 6</span>&nbsp;

            <div class="shop-item-details">

<span class="shop-item-price">£19.50</span>

            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>

        </div>

  

   <div class="shop-items">

        <div class="shop-item">

            <span class="shop-item-title">Album 8</span>&nbsp;

            <div class="shop-item-details">

<span class="shop-item-price">£19.50</span>

            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>

        </div>



翻翻过去那场雪
浏览 52回答 2
2回答

炎炎设计

您的意思是平滑滚动到该元素吗?您可以使用scrollIntoView():<!DOCTYPE html><html><head>   </head><body><header class="main-header">    <nav class="main-nav nav">    </nav></header>    <div class="shop-items">        <div class="shop-item">            <span class="shop-item-title">Album 0</span>&nbsp;            <div class="shop-item-details"><span class="shop-item-price">£12.50</span>            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>        </div>  <div class="shop-item">        <span class="shop-item-title">Album 2</span>&nbsp;        <div class="shop-item-details">            <span class="shop-item-price">£14.50</span>                       <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>        </div>        <div class="shop-item">        <span class="shop-item-title">Album 4</span>&nbsp;        <div class="shop-item-details"><span class="shop-item-price">£19.50</span>        <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>    </div>        </div></section>    <div class="shop-items">        <div class="shop-item">            <span class="shop-item-title">Album 6</span>&nbsp;            <div class="shop-item-details"><span class="shop-item-price">£19.50</span>            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>        </div>     <div class="shop-items">        <div class="shop-item">            <span class="shop-item-title">Album 8</span>&nbsp;            <div class="shop-item-details"><span class="shop-item-price">£19.50</span>            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>        </div>              </div></section><section class="container content-section">    <h2 class="section-header">Basket</h2>  <div class="cart-row">        <span class="cart-item cart-header cart-column">ITEM</span>        <span class="cart-price cart-header cart-column">PRICE</span>        <span class="cart-quantity cart-header cart-column">QUANTITY</span>    </div>    <p></p> <div class="cart-items">    </div>    <div class="cart-total">        <strong class="cart-total-title">Total</strong>        <span class="cart-total-price">£0</span>    </div>    <button class="btn btn-primary btn-purchase" type="button">CHECKOUT</button></section><script>if (document.readyState == 'loading') {    document.addEventListener('DOMContentLoaded', ready)} else {    ready()}function ready() {    var removeCartItemButtons = document.getElementsByClassName('btn-danger')    for (var i = 0; i < removeCartItemButtons.length; i++) {        var button = removeCartItemButtons[i]        button.addEventListener('click', removeCartItem)    }    var quantityInputs = document.getElementsByClassName('cart-quantity-input')    for (var i = 0; i < quantityInputs.length; i++) {        var input = quantityInputs[i]        input.addEventListener('change', quantityChanged)    }    var addToCartButtons = document.getElementsByClassName('shop-item-button')    for (var i = 0; i < addToCartButtons.length; i++) {        var button = addToCartButtons[i]        button.addEventListener('click', addToCartClicked)    }    document.getElementsByClassName('btn-purchase')[0].addEventListener('click', purchaseClicked)}function purchaseClicked() {    alert('Thank you for your purchase')    var cartItems = document.getElementsByClassName('cart-items')[0]    while (cartItems.hasChildNodes()) {        cartItems.removeChild(cartItems.firstChild)    }    updateCartTotal()}function removeCartItem(event) {    var buttonClicked = event.target    buttonClicked.parentElement.parentElement.remove()    updateCartTotal()}function quantityChanged(event) {    var input = event.target    if (isNaN(input.value) || input.value <= 0) {        input.value = 1    }    updateCartTotal()}function addToCartClicked(event) {    var button = event.target    var shopItem = button.parentElement.parentElement    var title = shopItem.getElementsByClassName('shop-item-title')[0].innerText    var price = shopItem.getElementsByClassName('shop-item-price')[0].innerText    addItemToCart(title, price)    updateCartTotal()}function addItemToCart(title, price) {    var cartRow = document.createElement('div')    cartRow.classList.add('cart-row')    var cartItems = document.getElementsByClassName('cart-items')[0]    var cartItemNames = cartItems.getElementsByClassName('cart-item-title')    for (var i = 0; i < cartItemNames.length; i++) {        if (cartItemNames[i].innerText == title) {            alert('This item is already added to the cart')            return        }    }    var cartRowContents = `        <div class="cart-item cart-column">            <span class="cart-item-title">${title}</span>        </div>        <span class="cart-price cart-column">${price}</span>        <div class="cart-quantity cart-column">            <input class="cart-quantity-input" type="number" value="1">            <button class="btn btn-danger" type="button">REMOVE</button>        </div>`    cartRow.innerHTML = cartRowContents    cartItems.append(cartRow)    cartRow.getElementsByClassName('btn-danger')[0].addEventListener('click', removeCartItem)    cartRow.getElementsByClassName('cart-quantity-input')[0].addEventListener('change', quantityChanged);    document.getElementsByClassName('btn-purchase')[0].scrollIntoView({ block: 'end',  behavior: 'smooth' });}function updateCartTotal() {var cartItemContainer = document.getElementsByClassName('cart-items')[0]var cartRows = cartItemContainer.getElementsByClassName('cart-row')var total = 0for (var i = 0; i < cartRows.length; i++) {    var cartRow = cartRows[i]    var priceElement = cartRow.getElementsByClassName('cart-price')[0]    var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]    var price = parseFloat(priceElement.innerText.replace('£', ''))    var quantity = quantityElement.value    total = total + (price * quantity)}total = Math.round(total * 100) / 100document.getElementsByClassName('cart-total-price')[0].innerText = '£' + total.toFixed(2)}</script></body></html>

www说

您可以使用 document.getElementById('yourbasket').scrollIntoView();if (document.readyState == 'loading') {&nbsp; &nbsp; document.addEventListener('DOMContentLoaded', ready)} else {&nbsp; &nbsp; ready()}function ready() {&nbsp; &nbsp; var removeCartItemButtons = document.getElementsByClassName('btn-danger')&nbsp; &nbsp; for (var i = 0; i < removeCartItemButtons.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var button = removeCartItemButtons[i]&nbsp; &nbsp; &nbsp; &nbsp; button.addEventListener('click', removeCartItem)&nbsp; &nbsp; }&nbsp; &nbsp; var quantityInputs = document.getElementsByClassName('cart-quantity-input')&nbsp; &nbsp; for (var i = 0; i < quantityInputs.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var input = quantityInputs[i]&nbsp; &nbsp; &nbsp; &nbsp; input.addEventListener('change', quantityChanged)&nbsp; &nbsp; }&nbsp; &nbsp; var addToCartButtons = document.getElementsByClassName('shop-item-button')&nbsp; &nbsp; for (var i = 0; i < addToCartButtons.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var button = addToCartButtons[i]&nbsp; &nbsp; &nbsp; &nbsp; button.addEventListener('click', addToCartClicked)&nbsp; &nbsp; }&nbsp; &nbsp; document.getElementsByClassName('btn-purchase')[0].addEventListener('click', purchaseClicked)}function purchaseClicked() {&nbsp; &nbsp; alert('Thank you for your purchase')&nbsp; &nbsp; var cartItems = document.getElementsByClassName('cart-items')[0]&nbsp; &nbsp; while (cartItems.hasChildNodes()) {&nbsp; &nbsp; &nbsp; &nbsp; cartItems.removeChild(cartItems.firstChild)&nbsp; &nbsp; }&nbsp; &nbsp; updateCartTotal()}function removeCartItem(event) {&nbsp; &nbsp; var buttonClicked = event.target&nbsp; &nbsp; buttonClicked.parentElement.parentElement.remove()&nbsp; &nbsp; updateCartTotal()}function quantityChanged(event) {&nbsp; &nbsp; var input = event.target&nbsp; &nbsp; if (isNaN(input.value) || input.value <= 0) {&nbsp; &nbsp; &nbsp; &nbsp; input.value = 1&nbsp; &nbsp; }&nbsp; &nbsp; updateCartTotal()}function addToCartClicked(event) {&nbsp; &nbsp; var button = event.target&nbsp; &nbsp; var shopItem = button.parentElement.parentElement&nbsp; &nbsp; var title = shopItem.getElementsByClassName('shop-item-title')[0].innerText&nbsp; &nbsp; var price = shopItem.getElementsByClassName('shop-item-price')[0].innerText&nbsp; &nbsp; addItemToCart(title, price)&nbsp; &nbsp; updateCartTotal()}function addItemToCart(title, price) {&nbsp; &nbsp; var cartRow = document.createElement('div')&nbsp; &nbsp; cartRow.classList.add('cart-row')&nbsp; &nbsp; var cartItems = document.getElementsByClassName('cart-items')[0]&nbsp; &nbsp; var cartItemNames = cartItems.getElementsByClassName('cart-item-title')&nbsp; &nbsp; for (var i = 0; i < cartItemNames.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (cartItemNames[i].innerText == title) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('This item is already added to the cart')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; var cartRowContents = `&nbsp; &nbsp; &nbsp; &nbsp; <div class="cart-item cart-column">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="cart-item-title">${title}</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <span class="cart-price cart-column">${price}</span>&nbsp; &nbsp; &nbsp; &nbsp; <div class="cart-quantity cart-column">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input class="cart-quantity-input" type="number" value="1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-danger" type="button">REMOVE</button>&nbsp; &nbsp; &nbsp; &nbsp; </div>`&nbsp; &nbsp; cartRow.innerHTML = cartRowContents&nbsp; &nbsp; cartItems.append(cartRow)&nbsp; &nbsp; cartRow.getElementsByClassName('btn-danger')[0].addEventListener('click', removeCartItem)&nbsp; &nbsp; cartRow.getElementsByClassName('cart-quantity-input')[0].addEventListener('change', quantityChanged)&nbsp; &nbsp; document.getElementById('yourbasket').scrollIntoView();}function updateCartTotal() {var cartItemContainer = document.getElementsByClassName('cart-items')[0]var cartRows = cartItemContainer.getElementsByClassName('cart-row')var total = 0for (var i = 0; i < cartRows.length; i++) {&nbsp; &nbsp; var cartRow = cartRows[i]&nbsp; &nbsp; var priceElement = cartRow.getElementsByClassName('cart-price')[0]&nbsp; &nbsp; var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]&nbsp; &nbsp; var price = parseFloat(priceElement.innerText.replace('£', ''))&nbsp; &nbsp; var quantity = quantityElement.value&nbsp; &nbsp; total = total + (price * quantity)}total = Math.round(total * 100) / 100document.getElementsByClassName('cart-total-price')[0].innerText = '£' + total.toFixed(2)}<!DOCTYPE html><html><head>&nbsp; &nbsp;</head><body><header class="main-header">&nbsp; &nbsp; <nav class="main-nav nav">&nbsp; &nbsp; </nav></header>&nbsp; &nbsp; <div class="shop-items">&nbsp; &nbsp; &nbsp; &nbsp; <div class="shop-item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="shop-item-title">Album 0</span>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="shop-item-details"><span class="shop-item-price">£12.50</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp;<div class="shop-item">&nbsp; &nbsp; &nbsp; &nbsp; <span class="shop-item-title">Album 2</span>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <div class="shop-item-details">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="shop-item-price">£14.50</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <div class="shop-item">&nbsp; &nbsp; &nbsp; &nbsp; <span class="shop-item-title">Album 4</span>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <div class="shop-item-details"><span class="shop-item-price">£19.50</span>&nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </div></section>&nbsp; &nbsp; <div class="shop-items">&nbsp; &nbsp; &nbsp; &nbsp; <div class="shop-item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="shop-item-title">Album 6</span>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="shop-item-details"><span class="shop-item-price">£19.50</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp;&nbsp; &nbsp;<div class="shop-items">&nbsp; &nbsp; &nbsp; &nbsp; <div class="shop-item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="shop-item-title">Album 8</span>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="shop-item-details"><span class="shop-item-price">£19.50</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; </div></section><section id="yourbasket" class="container content-section">&nbsp; &nbsp; <h2 class="section-header">Basket</h2>&nbsp; <div class="cart-row">&nbsp; &nbsp; &nbsp; &nbsp; <span class="cart-item cart-header cart-column">ITEM</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class="cart-price cart-header cart-column">PRICE</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class="cart-quantity cart-header cart-column">QUANTITY</span>&nbsp; &nbsp; </div>&nbsp; &nbsp; <p></p>&nbsp;<div class="cart-items">&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="cart-total">&nbsp; &nbsp; &nbsp; &nbsp; <strong class="cart-total-title">Total</strong>&nbsp; &nbsp; &nbsp; &nbsp; <span class="cart-total-price">£0</span>&nbsp; &nbsp; </div>&nbsp; &nbsp; <button class="btn btn-primary btn-purchase" type="button">CHECKOUT</button></section></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5