简体   繁体   English

仅当使用Django中的JavaScript单击相应的左/右箭头时,才激活特定的“图像滑块”

[英]Activate specific 'image slider' only when corresponding left/right arrow is clicked using JavaScript in Django

I am working on a Django website where customers can book mobile barbers for appointments in their own home. 我正在Django网站上工作,客户可以在该网站上预订移动理发店以便在家中预约。

I am a complete novice at JavaScript, but, as you can see from the images below, I have successfully created a working image slider for 'Johanson G' so customers can see demos of his haircuts when they click the left or right arrow on his profile. 我是JavaScript的新手,但是,正如您从下面的图片中可以看到的那样,我已经成功地为“ Johanson G”创建了一个有效的图片滑块,以便客户在单击其左或右箭头时可以看到其发型的演示。轮廓。

The problem I am having is: 我遇到的问题是:

1) The JS image slider code only seems to be applying to the first barber profile on the page, in this case, 'Johnason G'. 1)JS图像滑块代码似乎仅适用于页面上的第一个理发师资料,在本例中为“ Johnason G”。 As you can see 'William Despard's' images aren't even being hidden and are displaying below his profile picture. 如您所见,“ William Despard”的图像甚至没有被隐藏,而是显示在他的个人资料图片下方。

2) I don't know how to specify that when a specific barber profile's left or right arrow is clicked only THAT profiles image slider is activated. 2)我不知道如何指定当单击特定理发师配置文件的左箭头或右箭头时,仅激活“那个”配置文件图像滑块。 For example, I don't want every barber's demo images being displayed when an arrow is clicked on just one barber profile. 例如,我不希望仅在一个理发师资料上单击箭头就显示每个理发师的演示图像。 I imagine the solution for this will be including {{BarberProfile.id}} in each image's and arrow's 'id' and then somehow creating a rule where only the corresponding slider activated when the arrow is clicked? 我想解决方案将在每个图像和箭头的“ id”中包含{{BarberProfile.id}},然后以某种方式创建一个规则,当单击箭头时,仅激活相应的滑块?

Any help or insight would be much appreciated. 任何帮助或见解将不胜感激。 But, please remember I am a complete beginner at JS so please be detailed in your answers, don't assume I know anything! 但是,请记住我是JS的完整入门者,因此请在您的答案中进行详细说明,不要以为我什么都不知道!

在此处输入图片说明

JS code: JS代码:

let sliderImages = document.querySelectorAll(".slide"),
    arrowLeft = document.querySelector("#arrow-left"),
    arrowRight = document.querySelector("#arrow-right"),
    current = 0;

//clear all images
function reset(){
    for(let i = 0; i < sliderImages.length; i++){
        sliderImages[i].style.display = 'none';
    }
}

//initialises slider
function startSlide(){
    reset();
    sliderImages[0].style.display = 'block';
}

//show prev
function slideLeft(){
    reset();
    sliderImages[current - 1].style.display = 'block';
    current--;
}

//show next
function slideRight(){
    reset();
    sliderImages[current + 1].style.display = 'block';
    current++;
}

//left arrow clicked
arrowLeft.addEventListener('click', function(){
    if(current === 0){
        current = sliderImages.length;
    }
    slideLeft();
});

//right arrow clicked
arrowRight.addEventListener('click', function(){
    if(current === sliderImages.length - 1){
        current = - 1;
    }
    slideRight();
});

startSlide();

HTML: HTML:

<div id="total-div" class="margin-left-1 margin-right-1 margin-top-2 margin-bottom-2">

    <div style="width:50%;position:absolute;left: 50%;transform: translateX(-50%);z-index:1;pointer-events: none">
        <div style="padding-top: 100%;">
            <img id="arrow-left" class="arrow" style="pointer-events: auto;" src="{% static "img/arrow-2.svg" %}" alt="Mobile menu arrow">
            <img id="arrow-right" class="arrow" style="pointer-events: auto;" src="{% static "img/arrow-2.svg" %}" alt="Mobile menu arrow">
        </div>
    </div>


    <a class="profile-div" id="{{BarberProfile.id}} profile-div" target="{{BarberProfile.booking_link}}" onClick="toggle_content(this.id)">

        <!-- images -->
        <div id="image" class="slide slide1 border-radius-2 shadow" style="background-image: url({{BarberProfile.image.url}})">
            <div id="image-structure"></div>
        </div>
        <div id="image" class="slide slide2 border-radius-2 shadow" style="background-image: url({{BarberProfile.image_1.url}})">
            <div id="image-structure"></div>
        </div>
        <div id="image" class="slide slide3 border-radius-2 shadow" style="background-image: url({{BarberProfile.image_2.url}})">
            <div id="image-structure"></div>
        </div>
        <!-- images -->

        <h4 class="color-1 margin-top-2-lh margin-bottom-2-lh">{{BarberProfile.first_name}} {{BarberProfile.last_name}}</h4>
        <p class="color-4"> {{BarberProfile.bio}} </p>
        <div>
            <div id="text-div">
                <img id="text-image" class="margin-top-2" src="{% static "img/experience.svg" %}" alt="Mobile barber or hairdresser experience icon">
                <p id="text" class="color-4 margin-top-2">{{BarberProfile.years_of_experience}}</p>
            </div>
        </div>
        <div>
            <div id="text-div">
                <img id="text-image" class="margin-top-1" src="{% static "img/scissors.svg" %}" alt="Mobile barber or hairdresser experience icon">
                <p id="text" class="color-4 margin-top-1">{{BarberProfile.types}}</p>
            </div>
        </div>
    </a>

</div>

CSS: CSS:

#arrow-left{
    width: 25px;
    position: absolute;
    margin-top: -12.5px;
    top: 50%;
    left: -37.5px;
    transform: rotate(90deg);
    cursor: pointer;
}

#arrow-right{
    width: 25px;
    position: absolute;
    margin-top: -12.5px;
    top: 50%;
    right: -37.5px;
    transform: rotate(270deg);
    cursor: pointer;
}

#total-div{
    position: relative;
    cursor: pointer; /* turns mouse into pointer on hover*/
}
    #image{
        background-size: cover; /* makes barber image div background */
        border-radius: 50%; /* makes barber image circular */
        width: 50%;
        margin: auto;
    }
        #image-structure{
            padding-top: 100%; /* makes barber image height and width the same on size change */
        }
    #text-div{
        display:inline-block;
    }
        #text-image{
            height: 23.75px; /* same height as <p> line-height */
            float: left; /* keeps img on same line as <p> */
        }
        #text{
            margin-left: 33.125px; /* gives 12.5px space to the left */
        }

One of the things that you're going to want to do is have some way to distinguish which set of arrows is being clicked. 您要做的一件事情是使用某种方法来区分单击了哪组箭头。 I would start by moving arrow-left and arrow-right from 'id' to 'class'. 我将先从“ id”向“ class”移动左箭头和右箭头。 It's generally best practice to have any type of identifier, that is used more than once, be a class rather than an id. 通常,最好的做法是使用任何类型的标识符(不止一次使用)作为类而不是ID。

 $('.arrow').bind('click', function(){ $(this).siblings('.slide').html('clicked'); }) 
 .slider-container { display: flex; justify-content: space-around; } #slider-1 { background-color: red; } #slider-2 { background-color: blue; } .arrow { cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="slider-1" class="slider-container"> <div class="arrow arrow--left"> LEFT </div> <div class="slide"> 1 </div> <div class="arrow arrow--right"> RIGHT </div> </div> <div id="slider-2" class="slider-container"> <div class="arrow arrow--left"> LEFT </div> <div class="slide"> 1 </div> <div class="arrow arrow--right"> RIGHT </div> </div> 

This is admittedly a very stripped down version of what you're asking for. 诚然,这是您所要求的精简版本。 If you need further clarification I'd be glad to help. 如果您需要进一步的说明,我们将很乐意为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM