Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

javascript - 如何使用jQuery查找具有动态更新的active类而没有click事件的元素?(How do I use jQuery to find the element with a dynamically updated `active` class without click event?)

I have a materialize css carousel on my webpage.

(我的网页上有一个实现CSS轮播。)

When the carousel is rotated to the previous/next link, the class on the link in focus changes from class = "carousel-item active" to class = "carousel-item" .

(当轮播旋转到上一个/下一个链接时,焦点上的链接上的类将从class = "carousel-item active"变为class = "carousel-item" 。)

My goal is to use jQuery to identify the new active class and display corresponding text related to that image.

(我的目标是使用jQuery识别新的active类并显示与该图像有关的相应文本。)

I have tried using .mousemove() event listener to no avail.

(我尝试使用.mousemove()事件监听器无济于事。)

I am testing this by logging the itemid of the image in focus to the console.

(我正在通过将图像的itemid重点记录到控制台进行测试。)

On page load, the correct itemid is logged.

(在页面加载时,将记录正确的itemid 。)

However, when I scroll the carousel to the left or right, the new itemid does not log and nothing happens.

(但是,当我向左或向右滚动轮播时,新的itemid不会记录,什么也不会发生。)

I have also tried using hasClass() .

(我也尝试过使用hasClass() 。)

My confusion is not with how to identify or grab the element.

(我的困惑不在于如何识别或抓取元素。)

It is how to have the selector continuously listening so that when one image loses the active class and the other now has the active class, the corresponding text will display.

(如何使选择器连续监听,以便当一个图像失去active类别而另一个图像失去active类别时,将显示相应的文本。)

Here is a link to the projects page I am referencing: http://www.alexandervellios.com/projects.html

(这是我引用的项目页面的链接: http : //www.alexandervellios.com/projects.html)

Here is a code snippet of my carousel:

(这是我的轮播的代码段:)

  <div class= "carousel" id="fs">
        <a class="carousel-item" href="https://butterflysocial.herokuapp.com/"><img src="../img/butterfly.jpg" itemid="1" class="responsive-img">
            <h6 class= "carouselTitle white-text">Butterfly Social</h6>
        </a>
        <a class="carousel-item" href="https://bleauwonder.github.io/renegades-of-silicon-alley/"><img src="../img/compass.jpg" itemid="2" class="responsive-img">
            <h6 class= "carouselTitle white-text">Renegade TrailBrews</h6>
        </a>
    </div>

Here is an image of the code on the browser with the active class

(这是active类在浏览器上的代码图像)

UPDATE: I have added the click() event listener to my jQuery and the code is now registering a change.

(更新:我已经将click()事件侦听器添加到jQuery中,并且代码现在正在注册更改。)

However, it is still only logging the initial itemid of 1. Am I not grabbing the child element appropriately?

(但是,它仍然只记录初始itemid 。我是否没有适当地获取child元素?)

Here is the updated jQuery using the .click() event listener:

(这是使用.click()事件监听器的更新后的jQuery:)

$(".carousel").click(function( event ) {
  if($("a.carousel-item").hasClass("active")) {
    let itemId = $("img.responsive-img").attr("itemid")
    console.log("ID: ", itemId)
  }
})
  ask by Alexander translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After clicking the carousel, I can observe a short delay until the element holding the 'active' class changes.

(单击轮播之后,我可以观察到短暂的延迟,直到拥有“活动”类的元素发生变化。)

I imagine the class applies after the specified transition of the carousel's scroll animation.

(我想象该类在轮播的滚动动画的指定过渡之后适用。)

(Some information on that here: https://materializecss.com/carousel.html )

((有关此信息,请参见: https : //materializecss.com/carousel.html ))

I can't see an event listener provided by materialize for carousel scroll animation completion, so you might have to create your own workaround.

(我看不到materialize为轮播滚动动画完成提供的事件侦听器,因此您可能必须创建自己的解决方法。)

You could use setTimeout on your 'click' event listener, to check which element contains the 'active' class after the transition period.

(您可以在“点击”事件监听器上使用setTimeout,以检查过渡期之后哪个元素包含“活动”类。)

(But setTimeout is not accurately timed, so make sure you add a 100ms or so.)

((但是setTimeout的计时不准确,因此请确保添加100ms左右。))

function activeElementChanged() {
    let newElement = $("a.carousel-item.active")
    let itemId = newElement.find("img.responsive-img").attr("itemid")
    console.log("ID: ", itemId)
}

$(".carousel").click(function( event ) {
    setTimeout(activeElementChanged, 400)
}

Or you could use requestAnimationFrame to iterate continuously, checking if the active element has changed without listening for a click and without freezing the page's main thread.

(或者,您可以使用requestAnimationFrame进行连续迭代,检查活动元素是否已更改,而无需侦听单击,也无需冻结页面的主线程。)

let activeElement = null

function activeElementChanged(newElement) {
    activeElement = newElement
    let itemId = newElement.find("img.responsive-img").attr("itemid")
    console.log("ID: ", itemId)
}

let activeElementListener = function () {
    let currentActiveElement = $("a.carousel-item.active")
    if(currentActiveElement !== activeElement) {
        activeElementChanged(currentActiveElement)
    }
    requestAnimationFrame(activeElementListener)
}

requestAnimationFrame(activeElementListener)

EDIT: I just noticed you can get the index of the currently centered item using the materlize API.

(编辑:我刚刚注意到您可以使用materlize API获取当前居中项的索引。)

This should update straight after clicking without a delay.

(单击后应立即更新。)

I also fixed some issues with the previous code.

(我还修复了先前代码的一些问题。)

let carouselElement = $(".carousel")
let carousel = M.Carousel.getInstance(carouselElement);
let numberOfItems = ...

$(".carousel").click(function( event ) {
    let itemIndex = carousel.center
    while(itemIndex < 0)
        itemIndex += numberOfItems
    while(itemIndex >= numberOfItems)
        itemIndex -= numberOfItems
    let itemId = itemIndex + 1
    console.log("ID: ", itemId)
}

The 'center' property starts at zero and doesn't cycle (it goes infinitely negative as you scroll left, and infinitely positive as you go right), so you have to adjust it as shown above to get the itemId you are looking for.

(“居中”属性从零开始并且不循环(当您向左滚动时,它变为无限负数,而当您向右滚动时,它变为无限正数),因此您必须如上所示对其进行调整,以获取所需的itemId。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...