使用JavaScript从自动播放轮播中获取当前元素的文本。

huangapple go评论69阅读模式
英文:

Get text from current element in autoplay carousel using javascript

问题

我正在WordPress中使用Wow Carousel,并希望获取滑块中当前元素的文本。
在Wow Carousel中,当前幻灯片会带有类名"center"。
我的问题是,自动播放滑块中存储在我的变量中的值不会自动更改。
如何在每次自动添加类名"center"时触发该函数?
我的代码如下:

<div id="testimonial-slider-5781" class="owl-carousel owl-loaded owl-drag">
  <div class="owl-item active" style="width: 235.333px; margin-right: 15px;"><div class="testimonial-5781">
    <h3>文本</h3>
  </div></div>
  <div class="owl-item active" style="width: 235.333px; margin-right: 15px;"><div class="testimonial-5781">
    <h3>文本</h3>
  </div></div>
  <div class="owl-item active center" style="width: 235.333px; margin-right: 15px;"><div class="testimonial-5781">
    <h3>文本</h3>
  </div></div>
  <div class="owl-item" style="width: 235.333px; margin-right: 15px;"><div class="testimonial-5781">
    <h3>文本</h3>
  </div></div>
</div>

jQuery(document).ready(function() {
  function myFunction() {
    let title = jQuery("#testimonial-slider-5781 .center .testimonial-5781 h4").text();
    jQuery("#service-title h3").text(title);
  }

  jQuery('.owl-prev').click(function(){
    myFunction();
  });

  jQuery('.owl-next').click(function(){
    myFunction();
  });
});
英文:

I'm using Wow carousel in WordPress, and I want to get the text in the current element in the slider.
In Wow Carousel, the current slide takes the class center
My problem with the autoplay slider, the value stored in my variable doesn't change automatically
How can I fire the function when the class center is added automatically every time?
My code

&lt;div id=&quot;testimonial-slider-5781&quot; class=&quot;owl-carousel owl-loaded owl-drag&quot;&gt;
&lt;div class=&quot;owl-item active&quot; style=&quot;width: 235.333px; margin-right: 15px;&quot;&gt;&lt;div class=&quot;testimonial-5781&quot;&gt;
&lt;h3&gt; text &lt;/h3&gt;
&lt;/div&gt;
&lt;div class=&quot;owl-item active&quot; style=&quot;width: 235.333px; margin-right: 15px;&quot;&gt;&lt;div class=&quot;testimonial-5781&quot;&gt;
&lt;h3&gt; text &lt;/h3&gt;
&lt;/div&gt;
&lt;div class=&quot;owl-item active center&quot; style=&quot;width: 235.333px; margin-right: 15px;&quot;&gt;&lt;div class=&quot;testimonial-5781&quot;&gt;
&lt;h3&gt; text &lt;/h3&gt;
&lt;/div&gt;
&lt;div class=&quot;owl-item&quot; style=&quot;width: 235.333px; margin-right: 15px;&quot;&gt;&lt;div class=&quot;testimonial-5781&quot;&gt;
&lt;h3&gt; text &lt;/h3&gt;
&lt;/div&gt;
&lt;/div&gt;
jQuery(document).ready(function() {
	function myFunction() {
	let title = jQuery( &quot;#testimonial-slider-5781 .center .testimonial-5781 h4&quot; ).text();
	jQuery(&quot;#service-title h3&quot;).text(title);
	}   
	jQuery(&#39;.owl-prev&#39;).click(function(){
		myFunction();
	});
	jQuery(&#39;.owl-next&#39;).click(function(){
		myFunction();
	});
});

答案1

得分: 2

你可以监听 translated.owl.carousel 事件,因为它在每次轮播被翻译时触发,这意味着它也会在当前幻灯片添加类 center 时触发。

以下是一个示例,根据您自己的代码进行适当的调整。

jQuery(document).ready(function() {
  function myFunction() {
    let title = jQuery("#testimonial-slider-5781 .center .testimonial-5781 h3").text();
    jQuery("#service-title h3").text(title);
  }   
  jQuery("#testimonial-slider-5781").on("translated.owl.carousel", function() {
    myFunction();
  });
});
英文:

You can listen to translated.owl.carousel event since it's triggered everytime carousel is translated, that would mean it's also triggered when class center is added to current slide.

Here is an example, do adapt to your own code as appropriate.

jQuery(document).ready(function() {
  function myFunction() {
    let title = jQuery(&quot;#testimonial-slider-5781 .center .testimonial-5781 h3&quot;).text();
    jQuery(&quot;#service-title h3&quot;).text(title);
  }   
  jQuery(&quot;#testimonial-slider-5781&quot;).on(&quot;translated.owl.carousel&quot;, function() {
    myFunction();
  });
});

huangapple
  • 本文由 发表于 2023年2月18日 21:44:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定