English 中文(简体)
SvelteKit alternative way to display component base on screen width
原标题:

I use this to get the current window size in my sveltekit app, and change the component being displayed when the width is small:

<script>
let size;
</script>
<svelte:window bind:innerwidth{size}/>
<div>
    {#if size > 768}
        <TableDisplay/>
    {:else}
        </CardsDisplay/>
    {/if}
</div>

Is there any other way to do this?

问题回答

Media queries would be an option.

You would define classes that show/hide an element depending on viewport width. This requires a wrapper element that the style can be applied to, or you define the rules as global, pass the class into the components and add them there.

E.g.

<div class="show-large">
    <TableDisplay/>
</div>
<div class="show-small">
    <CardsDisplay/>
</div>

<style>
  /* `contents` prevents wrapper from affecting layout */
  .show-small { display: none; }
  .show-large { display: contents; }

  @media (max-width: 768px) {
    .show-small { display: contents; }
    .show-large { display: none; }
  }
</style>

REPL Example

Note that this does behave differently from the {#if} approach: With media queries, all elements and components are created and inserted into the document, just not always shown, while {#if} does not create anything if the condition is not met.





相关问题
select multiple files in file upload in php? [duplicate]

Possible Duplicate: How to select multiple files for upload? i know that php doesn t allow you to select multiple files to upload in one box. is there a work around or do you have to learn ...

Connecting Actionscript 3.0 with a C++ backend?

I am curious to know if there is a way of connecting a flash front-end to a C++ driven backend? I m not currently working on a project that involves this, but I found out about an application used in ...

PHP searchable catalog design

I m working on a PHP Real Estate catalog. I am working on the search part right now and I m having trouble with the logic. Should the default view show all items in the catalog or the search page? ...

jquery image preview that work on ajax embedded image links?

there are various good jquery image preview plugins out there. however, all of the ones i ve tested havent worked on image links that are embedded in the DOM with ajax. i have tested use jquery live ...

Why should I use XFN in my HTML?

What is the benefit of using XFN (XHTML Friends Network)? I ve seen this on multiple blogs and social networking sites but I don t really understand why it s useful. Other than being able to style ...

The modern way to clear floated content?

What s the modern way to clear floated content these days? There s the "recent" modern way of adding a ".clearfix" on the parent element to clear the contained floats and that would work great. In ...

create a "show up" window when you click register?

im a backend programmer who wants to have a window that appears in front of the current window when clicking "register". so that you dont have to redirect to another page. i think u guys know what i ...

热门标签