简体   繁体   English

使用jQuery将一个元素从一个位置移动到另一个位置

[英]Move one element from one spot to another using jQuery

I'm using Foundation 3 to build a site and am using the orbit slider that comes with it. 我正在使用Foundation 3来构建站点,并正在使用它附带的轨道滑块 The problem is that my site design requires me to move the slider navigation into a different location than is allowed by the slide. 问题是我的网站设计要求我将滑块导航移动到与幻灯片允许的位置不同的位置。

My source code is: 我的源代码是:

<div id="slide-wrapper">
  <div class="orbit-wrapper" style="width: 1384px; height: 560px;"><div class="slider orbit" style="width: 1384px; height: 560px;">
      <img src="http://domain.com/uploads/sliders/psd1400x558.jpg" alt="slide image" data-caption="#captionId1" class="orbit-slide" style="opacity: 1; z-index: 3; display: block;">
      <img src="http://domain.com/uploads/sliders/banner1.jpg" alt="slide image" data-caption="#captionId2" class="orbit-slide" style="opacity: 0; display: block;">
  </div>
  <div class="slider-nav hide-for-small">
      <span class="right">Right</span>
      <span class="left">Left</span>
  </div>
</div><!--slider-->

<div class="blue-wrapper">
    <div class="left-blue"></div>
    <div class="right-blue"></div>
</div>


<span class="orbit-caption" id="captionId1">
   <h3>Title one</h3>
   <p>Text copy text copy text copy </p>
</span>

<span class="orbit-caption" id="captionId2">
   <h3>Title the Second</h3>
   <p>text copy text copy text copy</p>
</span> 

I need to move the div.slider-nav inside each of the span.orbit-caption however what I've tried is not working as expected. 我需要在每个span.orbit标题内移动div.slider-nav,但是我尝试过的工作没有按预期进行。

My current jQuery is: 我当前的jQuery是:

// Move home slider navigation from .slider to .orbit-caption
$(document).ready(function() {
     $('div.slider-nav').detach().appendTo('span.orbit-caption');
});

Please advise. 请指教。

Try with this: 试试这个:

var slider = $('div.slider-nav');
$('span.orbit-caption').each(function(){
   $(this).append(slider.clone());
});

slider.remove();

Looking at the relevant bit of the orbit.js source , the event handlers for triggering the next/prev slides are bound using delegated events on $slides_container (ie the slider wrapper element) and then filter events bubbling up to this element by the next/prev html class names ( orbit-next / orbit-prev by default). 查看orbit.js源代码的相关位,触发下一张/上一张幻灯片的事件处理程序使用$slides_container上的委派事件(即,滑块包装器元素)绑定,然后过滤由next /冒泡到该元素的事件。上一页html类名称(默认为orbit-next / orbit-prev )。

This means that if you move the prev/next controls out of the slider wrapper they will no longer work, as click events will no longer bubble up to the wrapper (as they are no longer children of it). 这意味着,如果将上一个/下一个控件从滑块包装中移出,它们将不再起作用,因为单击事件将不再冒泡到包装中(因为它们不再是包装的子级)。

First pass: hackety-hackety-hack 第一遍:hackety-hackety-hack

A hacky workaround would be to insert new prev/next links wherever you like, and then bind new event handlers to simulate the click on the original next/prev controls (which could then be hidden as they aren't needed): 一个变通的解决方法是在您喜欢的地方插入新的上一个/下一个链接,然后绑定新的事件处理程序,以模拟对原始下一个/上一个控件的单击(然后可以将其隐藏,因为不需要它们):

// not ideal, don't do this unless the second method listed below doesn't work
$(document).ready(function() {
    var $captions = $('orbit-caption'),
        $orbit = $('#id-of-your-orbit'),
        $sliderNav = $orbit.find('slider-nav'),
        $oldNextBtn = $sliderNav.find('.left'),
        $oldPrevBtn = $sliderNav.find('.right'),
        $newNextBtns = $captions.find('.your-next-class'),
        $newPrevBtns = $captions.find('.your-prev-class');

    $sliderNav.hide();//hide it but leave in DOM so we can trigger clicks on it's contents below

    $newNextBtns.on('click', function(){
        $oldNextBtn.trigger('click');
    });

    $newPrevBtns.on('click', function(){
        $oldPrevBtn.trigger('click');
    });
});

A better way: tie into existing custom events 更好的方法:与现有的自定义事件关联

However, a much neater solution looks to be to just trigger the (undocumented AFAICS) orbit:next-slide or orbit:prev-slide events on the container element when the controls are clicked, with something like: 但是,更巧妙的解决方案是单击控件时在容器元素上触发(未记录的AFAICS) orbit:next-slideorbit:prev-slide事件:

$(document).ready(function() {
    var $captions = $('orbit-caption'),
        $orbit = $('#slide-wrapper'),
        $nextBtns = $captions.find('.your-next-class'),
        $prevBtns = $captions.find('.your-prev-class');

    $orbit.find('slider-nav').remove();

    $nextBtns.on('click', function(){
        $orbit.trigger('orbit:next-slide');
    });

    $prevBtns.on('click', function(){
        $orbit.trigger('orbit:prev-slide');
    });
});

NB I haven't tested that, but looking at the source it looks like it should work. 注意:我还没有测试过,但是从源头上看它应该可以工作。

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

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