javascript - 在类外定位类内的元素

标签 javascript jquery html css

所以我在这里使用这个演示插件 http://tympanus.net/Tutorials/PrettySimpleContentSlider/并试图让右侧的动画选项卡(ul id =“rotmenu”)能够自由 float 并在其主要 div 类“rotator”之外自由定位,并且仍然保持其功能并在其中发挥作用旋转器。我尝试用 div 和类的属性在这里和那里做一些简单的更改,但它们或多或少地把事情搞砸了。有任何想法吗?

好的,这是使用 jquery_easing.1.3 和 jquery.min.js 库的 js

<script type="text/javascript">
        $(function() {
            var current = 1;

            var iterate     = function(){
                var i = parseInt(current+1);
                var lis = $('#rotmenu').children('li').size();

            }
            display($('#rotmenu li:first'));
            var slidetime = setInterval(iterate,3000);

            $('#rotmenu li').bind('click',function(e){
                clearTimeout(slidetime);
                display($(this));
                e.preventDefault();
            });

            function display(elem){
                var $this   = elem;
                var repeat  = false;
                if(current == parseInt($this.index() + 1))
                    repeat = true;

                if(!repeat)
                    $this.parent().find('li:nth-child('+current+') a').stop(true,true).animate({'marginRight':'-20px'},300,function(){
                        $(this).animate({'opacity':'0.7'},700);
                    });

                current = parseInt($this.index() + 1);

                var elem = $('a',$this);

                    elem.stop(true,true).animate({'marginRight':'0px','opacity':'1.0'},300);

                var info_elem = elem.next();
                $('#rot1 .heading').animate({'left':'-420px'}, 500,'easeOutCirc',function(){
                    $('h1',$(this)).html(info_elem.find('.info_heading').html());
                    $(this).animate({'left':'0px'},400,'easeInOutQuad');
                });

                $('#rot1 .description').animate({'bottom':'-270px'},500,'easeOutCirc',function(){
                    $('p',$(this)).html(info_elem.find('.info_description').html());
                    $(this).animate({'bottom':'0px'},400,'easeInOutQuad');
                })
                $('#rot1').prepend(
                $('<img/>',{
                    style   :   'opacity:0',
                    className : 'bg'
                }).load(
                function(){
                    $(this).animate({'opacity':'1'},600);
                    $('#rot1 img:first').next().animate({'opacity':'0'},700,function(){
                        $(this).remove();
                    });
                }
              ).attr('src','images/'+info_elem.find('.info_image').html()).attr('width','800').attr('height','300')
            );
            }
        });
    </script>

应用jquery的地方

 <body>
    <div id="content">
        <a class="back" href=""></a>

        <div class="rotator">
            <ul id="rotmenu">
                <li>
                    <a href="rot1">Portfolio</a>
                    <div style="display:none;">
                        <div class="info_image">1.jpg</div>
                        <div class="info_heading">Our Works</div>
                        <div class="info_description">

                            <a href="#" class="more">Read more</a>
                        </div>
                    </div>
                </li>
                <li>
                    <a href="rot2">Services</a>
                    <div style="display:none;">
                        <div class="info_image">2.jpg</div>
                        <div class="info_heading">We serve</div>
                        <div class="info_description">

                            <a href="#" class="more">Read more</a>
                        </div>
                    </div>
                </li>
                <li>
                    <a href="rot3">Contact</a>
                    <div style="display:none;">
                        <div class="info_image">3.jpg</div>
                        <div class="info_heading">Get in touch</div>
                        <div class="info_description">

                            <a href="#" class="more">Read more</a>
                        </div>
                    </div>
                </li>
                <li>
                    <a href="rot4">Experiments</a>
                    <div style="display:none;">
                        <div class="info_image">4.jpg</div>
                        <div class="info_heading">We do crazy stuff</div>
                        <div class="info_description">

                            <a href="#" class="more">Read more</a>
                        </div>
                    </div>
                </li>
                <li>
                    <a href="rot5">Applications</a>
                    <div style="display:none;">
                        <div class="info_image">5.jpg</div>
                        <div class="info_heading">Working things</div>
                        <div class="info_description">

                            <a href="#" class="more">Read more</a>
                        </div>
                    </div>
                </li>
            </ul>
            <div id="rot1">
                <img src="" width="800" height="300" class="bg" alt=""/>
                <div class="heading">
                    <h1></h1>
                </div>
                <div class="description">
                    <p></p>

                </div>    
            </div>
        </div>
    </div>

最佳答案

如果您希望它在其父级(.rotator)之外可见,您需要更改父级的 CSS 使其具有 overflow: visible。然后您可以将 #rotmenu 放在任何您想要的位置。

#rotmenu {
    position: absolute;
    top: 300px;
    left: 100px;
    z-index: 10;
}    
.rotator {
    ...
    overflow:visible;
    ...
}

此外,为了继续遮蔽动画组件,需要将#rot1设置为overflow: hidden,并使其与common parent (.rotator)。

#rot1 {
    position: absolute;
    overflow: hidden;
    height: 100%;
    width: 100%;
}

最后,为了防止在 DOM 中收集损坏的图像(在示例中发生这种情况,因为我们不包含源图像),我在图像插入代码中添加了一个错误处理程序以删除图像。

$('#rot1').prepend(
$('<img/>', {
    style: 'opacity:0',
    className: 'bg'
}).error(function () {
    $(this).remove();
}).load(function () {
    $(this).animate({
        'opacity': '1'
    }, 600);
    $('#rot1 img:first').next().animate({
        'opacity': '0'
    }, 700, function () {
        $(this).remove();
    });
}).attr('src', 'images/' + info_elem.find('.info_image').html()).attr('width', '800').attr('height', '300'));

jsfiddle

关于javascript - 在类外定位类内的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14783436/

相关文章:

javascript - 有没有类似 Stackoverflow 的 "Please login or register"框的 jQuery 插件?

javascript - JSONP引用错误: Can't find variable

javascript - 如何将关闭按钮放在jquery弹出的标题栏中

javascript - 我在将值附加到数组时遇到问题

php - 从服务器端捕获错误

javascript - Bootstrap 导航栏子菜单事件状态不起作用

css - 在声明 html 5 DTD 时,一些 css 样式不计算任何浏览器

html - wordpress 中的 CSS Sprite

Javascript 弹出窗口不适用于下一页分页

javascript - JS计算完成后,下次显示NAN值