简体   繁体   English

如何使用jQuery在特定页面宽度上上下移动div的div

[英]How to move a div up/down the dom on certain page width with Jquery

I'm fairly new to Jquery and this is a bit out of my league. 我对Jquery还是很陌生,这有点超出我的范围了。

I want to move a div to a different location, which I can accomplish with: 我想将div移到其他位置,可以用以下方法完成:

$('#searchbar').insertAfter('.search-item');

Now what I want to do is write an if statement that says if the screen width is 800px do: 现在我想做的是编写一条if语句,说明屏幕宽度是否为800px,请执行以下操作:

$('#searchbar').insertAfter('.search-item');

else(screen width above 800px) put it back to its original location. else(屏幕宽度超过800像素)将其放回原始位置。

I've tried searching but it seems pretty specific. 我尝试搜索,但似乎很具体。

Any ideas or is there a better way to accomplish what I want? 有什么想法或有更好的方法来实现我想要的吗?

Thanks in advance for any help! 在此先感谢您的帮助!

You can do something like this with jQuery: 您可以使用jQuery执行以下操作:

$(window).on("resize", function() { //or $(window).resize(function() {
    var w = $(this).width();
    if (w < 800) {
        //do less than action
    } else {
        //do else action
    }
});

As one commentor mentioned above, you might be able to do something like this using CSS. 正如上面提到的一个评论者,您也许可以使用CSS来执行类似的操作。

You can get the index of the item in its parent container before you move it, then use screen width to move it back. 您可以在移动项目之前在其父容器中获取该项目的索引,然后使用屏幕宽度将其移回。

Something like this 像这样

var searchbar = $('#searchbar');

// Get the parent container of the bar.
var parent = searchbar.parent();

// Get the starting location of the bar.
var index = parent.index(searchbar);

if($(window).width() >= 800){
    // Insert the bar after the '.search-item'
    searchbar.insertAfter('.search-item');
}
else{
    // Insert the bar in its original location
    $(parent.children()[index]).before(searchbar);
}

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

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