溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

前端實現(xiàn)滑動按鈕AJAX與后端交互的代碼怎么寫

發(fā)布時間:2022-02-23 13:48:57 來源:億速云 閱讀:150 作者:iii 欄目:開發(fā)技術(shù)

這篇“前端實現(xiàn)滑動按鈕AJAX與后端交互的代碼怎么寫”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“前端實現(xiàn)滑動按鈕AJAX與后端交互的代碼怎么寫”文章吧。

html代碼

<div class="switch-box">
    <input id="switchButton" type="checkbox" class="switch" />
    <label for="switchButton"></label>
</div>

css代碼

.switch-box {
    width: 48px;
}
.switch-box .switch {
    /* 隱藏checkbox默認(rèn)樣式 */
    display: none;
}
.switch-box label {
    /* 通過label擴(kuò)大點擊熱區(qū) */
    position: relative;
    display: block;
    margin: 1px;
    height: 28px;
    cursor: pointer;
}
.switch-box label::before {
    /* before設(shè)置前滾動小圓球 */
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -13px;
    margin-left: -14px;
    width: 26px;
    height: 26px;
    border-radius: 100%;
    background-color: #fff;
    box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.06);
    /* 通過transform、transition屬性控制元素過渡進(jìn)而形成css3動畫 */
    -webkit-transform: translateX(-9px);
    -moz-transform: translateX(-9px);
    transform: translateX(-9px);
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
    transition: all 0.3s ease;
}
.switch-box .switch:checked~label::before {
    /* 語義:被選中的類名為"switch"元素后面的label元素里的偽類元素,進(jìn)行更改css樣式 */
    /* 形成偽類結(jié)構(gòu)選擇器:":"冒號加布爾值"checked" */
    /* " Ele1 ~ Ele2 "波浪號在css的作用:連接的元素必須有相同的父元素,選擇出現(xiàn)在Ele1后的Ele2(但不必跟在Ele1,也就是說可以并列)  */
    -webkit-transform: translateX(10px);
    -moz-transform: translateX(10px);
    transform: translateX(10px);
}
.switch-box label::after {
    /* after設(shè)置滾動前背景色 */
    content: "";
    display: block;
    border-radius: 30px;
    height: 28px;
    background-color: #dcdfe6;
    -webkit-transition: all 0.3s ease;
    -moz-transition: all 0.3s ease;
    transition: all 0.3s ease;
}
.switch-box .switch:checked~label::after {
    background-color: #13ce66;
}

效果圖

效果如圖:

前端實現(xiàn)滑動按鈕AJAX與后端交互的代碼怎么寫

JS事件觸發(fā)

<input id="switchButton" type="checkbox" class="switch" onclick="reverseStatus('1')" />

input添加onclick事件,點擊觸發(fā)reverseStatus()函數(shù)

<script>
	function reverseStatus(id){
		$.get("/pocs/reverse/"+id);
	}
</script>

flask后端接口

@poc.route('/pocs/reverse/<int:id>', methods=['GET'])
def reverse(id=None):
    print(id)
    return 'success'

在后端編寫我們需要的邏輯

以上就是關(guān)于“前端實現(xiàn)滑動按鈕AJAX與后端交互的代碼怎么寫”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI